Amanda Kitson
Amanda Kitson

Reputation: 5547

Can I call a php script from javascript without it being on a web server?

I have a need for a simple html + javascript page that must send multiple emails to a predefined list of users upon a button click.

I need the html page to be able to run locally on a machine that does not have a web server installed.

If it was just the html page by itself, this would be no problem. But from everything I've read, it is not possible to send emails directly from javascript/jquery (see this StackOverflow post here for example). Instead, I have to call some kind of script to actually send the emails.

I also looked at this post, but this one seems to be about running a self contained script, not invoking a method with parameters. I'm not sure that this is what I need.

I am looking for a way to send the emails without requiring that a web server be running on the machine. It doesn't have to be PHP. Is there any simple way I can do this without requiring a web server to exist on the machine?

Upvotes: 1

Views: 766

Answers (4)

Duane Gran
Duane Gran

Reputation: 490

You can run PHP without the web server and use it as a command line tool:

http://www.php.net/manual/en/features.commandline.usage.php

Your problem will be that you need some way of getting the "state" of your client/server information from JavaScript over there. You can't (easily) write files with JavaScript, but you can issue standard HTTP requests. For this you would need a web server.

You say that this will run stand alone on a workstation. You may find it easier to adjust the security permissions (see here) to write to a local file containing the email parameters and then use a cron job or scheduled task on the local machine to parse the file kick off an email.

Upvotes: 1

hohner
hohner

Reputation: 11588

You're going to need some form of server-side script to hook up to the SMTP server.

Because JavaScript doesn't support cross-domain requests, you can't $.post() to an external URL either...

Upvotes: 1

Paul Voss
Paul Voss

Reputation: 765

You need to trigger a server-side script in order to send emails.

If you have the option to use another webserver you could call the script via AJAX like this:

$.ajax({    
  url: "http://some.remote.server/send_email.php",
  data: {
    receipient: "[email protected]",
    subject: "Some Subject",
    body: "Hello Dude!"
  },
  success: function(){
     // ...
  }
});

Upvotes: -2

Adam Hepton
Adam Hepton

Reputation: 674

To answer your question: no, there is no way to run server-side code (which sending an email must be) without having a server to run that code - PHP, Python, Ruby or anything else.

You could look at using a third-party service like http://postageapp.com/ - but be aware that there'll be costs involved, and you'll have to route the content of those emails through that third-party service.

Upvotes: 4

Related Questions