Let's reveal the code!

Sending Emails using PHPMailer via SMTP

Send Email

Recently I used PHPMailer to send emails via SMTP. PHPMailer is easy to setup class in just a couple of minutes and its sample files are really good. The following steps show how to use it with SMTP for both Gmail and Amazon (SES) Simple Email Service.

  1. You can download the latest version of PHPMailer here.
  2. Include the PHPMailerclass in your script.

  3. Create the following function that uses PHPMailer object configured with settings required by Gmail. Then you can call it from within your script.
    Note: yourdomain in this script is your domain name that you host at Gmail.

  4. If you want to use Amazon (SES) Simple Email Servicefor your email hosting, then you can use the following settings instead.

  5. SMTP username and password used above are SMTP credentials you create in SMTP Settings tab in Amazon SES panel.
    You should verify the email used in setFrom function “from email” in Verified Senders tab in Amazon SES panel.
  6. Now both functions are ready for use according to your mail hosting service. 
  7. One last note is with the above script the reply to headers  will be to both the $from and $reply_to emails parameters. But if you want the email to be sent from one email and reply to another email, call the AddReplyTo function before SetFrom as in the following example