PHP script to connect to an SMTP server and send email in Windows 7
Sending email with PHP on Windows is a bit of a minefield with gotchas and head scratches. I will try to get you through one instance where I got it to work with Windows 7 and PHP 5.2.3 under (IIS) the Internet Information Services web server.
I assume that you do not want to use any pre-created frameworks, such as CodeIgniter or Symfony, that contain email sending capabilities. We will send an email from a standalone PHP file. I got this code from under codeigniter hood (under system / libraries) and modified it so that you can just look into 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 username / password with SMTP server:
I am using the smtp server from smtp.ihostexchange.net , which has already been created and configured for me. If you do not have this, you cannot proceed. You should be able to use an email client such as thunderbird, evolution, Microsoft Outlook to specify your smtp server, and then you can send emails there,
Step 2. Create your Hello World email file:
I assume you are using IIS. Therefore, create a file under index.php under C:\inetpub\wwwroot and place this code there:
<?php include("Email.php"); $c = new CI_Email(); $c->from("FromUserName@foobar.com"); $c->to("user_to_receive_email@gmail.com"); $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 going to localhost / index.php in the browser, this will lead to errors because Email.php is missing. But make sure you can at least run it from a browser.
Step 3, create a file called Email.php :
Create a new file called Email.php in the 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 types of smtp servers, you will have to manually configure the settings at the top of Email.php . I configured it so that it automatically works with smtp.ihostexchange.net , but your smtp server may be different.
For example:
- Set the smtp_port parameter to the port of your SMTP server.
- Set the smtp_crypto parameter for your smtp server.
- Install $ newline and $ crlf so that it is compatible with what your SMTP server uses. If you are mistaken, the smtp server can ignore your request without errors. I use \ r \ n, maybe
\n is required for you.
The linked code is too long to be inserted as a stackoverflow response. If you want to edit it, leave a comment here or via github, and I will change it.
Step 4, make sure your php.ini has the ssl extension:
Find the PHP.ini file and uncomment
It looks like this:
extension=php_openssl.dll
Step 5: run the index.php file that you just created in the 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 <534edd7c92761@summitbroadband.com> Queued mail for delivery lang:email_sent done
Step 6, check your email and spam folder:
Visit your email account for user_to_receive_email@gmail.com and you should receive an email. It should arrive within 5 or 10 seconds. If you do not, check the errors returned on the page. If this does not work, try stretching your face on the keyboard on Google, repeating: "Working at the grocery store is not so bad."