Sending an email from PHP in Windows is a bit of a minefield with gotchas and head scratching. I'll try to walk you through one instance where I got it to work on Windows 7 and PHP 5.2.3 under (IIS) Internet Information Services webserver.
I'm assuming you don't want to use any pre-built framework like CodeIgniter or Symfony which contains email sending capability. We'll be sending an email from a standalone PHP file. I acquired this code from under the codeigniter hood (under system/libraries) and modified it so you can just drop in this Email.php file and it should just work.
This should work with newer versions of PHP. But you never know.
Step 1, You need a username/password with an SMTP server:
I'm using the smtp server from smtp.ihostexchange.net
which is already created and setup for me. If you don't have this you can't proceed. You should be able to use an email client like thunderbird, evolution, Microsoft Outlook, to specify your smtp server and then be able to send emails through there.
Step 2, Create your Hello World Email file:
I'm assuming you are using IIS. So create a file called index.php under C:\inetpub\wwwroot
and put this code in there:
<?php
include("Email.php");
$c = new CI_Email();
$c->from("[email protected]");
$c->to("[email protected]");
$c->subject("Celestial Temple");
$c->message("Dominion reinforcements on the way.");
$c->send();
echo "done";
?>
You should be able to visit this index.php by navigating to localhost/index.php in a browser, it will spew errors because Email.php is missing. But make sure you can at least run it from the browser.
Step 3, Create a file called Email.php
:
Create a new file called Email.php under C:\inetpub\wwwroot
.
Copy/paste this PHP code into Email.php:
https://github.com/sentientmachine/standalone_php_script_send_email/blob/master/Email.php
Since there are many kinds of smtp servers, you will have to manually fiddle with the settings at the top of Email.php
. I've set it up so it automatically works with smtp.ihostexchange.net
, but your smtp server might be different.
For example:
\n
is required.The linked code is too long to paste as a stackoverflow answer, If you want to edit it, leave a comment in here or through github and I'll change it.
Step 4, make sure your php.ini has ssl extension enabled:
Find your PHP.ini file and uncomment the
;extension=php_openssl.dll
So it looks like:
extension=php_openssl.dll
Step 5, Run the index.php file you just made in a browser:
You should get the following output:
220 smtp.ihostexchange.net Microsoft ESMTP MAIL Service ready at
Wed, 16 Apr 2014 15:43:58 -0400 250 2.6.0
<[email protected]> Queued mail for delivery
lang:email_sent
done
Step 6, check your email, and spam folder:
Visit the email account for [email protected] and you should have received an email. It should arrive within 5 or 10 seconds. If you does not, inspect the errors returned on the page. If that doesn't work, try mashing your face on the keyboard on google while chanting: "working at the grocery store isn't so bad."