Using PHPMailer and Amazon SES

孤者浪人 提交于 2019-12-03 01:11:38

I managed to make it work.

$ mail-> Port = 587; 

// Username to use for SMTP authentication - use full email address for gmail 
$ mail-> Username = "Amazon SES Secret ID"; 

// Password to use for SMTP authentication 
$ mail-> Password = "Amazon SES Secret Key"; 

To create credentials visit this link: https://console.aws.amazon.com/ses/home?region=us-east-1#smtp-settings:

To send e-mail, both sending email, as the email you receive will necessarily have to be registered and checked by amazon. (https://console.aws.amazon.com/ses/home?region=us-east-1#verified-senders-mail:)

Any questions you can find me. I hope I helped

I had the same problem, I could only get it to work when I changed host name to start with ssl://. So this works:

$mail = new PHPMailer;

$mail->SMTPDebug = 3; // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'ssl://email-smtp.us-west-2.amazonaws.com';  // Specify main and backup SMTP servers

$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'blah';                 // SMTP username
$mail->Password = 'blahblah';                           // SMTP password


$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 443;                  

To send email successfully with PHPMailer + AWS SES, I had to use these settings:

$mail->IsSMTP(true);
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host= "email-smtp.eu-west-1.amazonaws.com"; /** Edit to the region you're using **/
$mail->Port = 587;
$mail->Username = 'MYACCESSKEYID';
$mail->Password = 'MYSECRETACCESSKEY'

Taking it one step further, I extended the PHPMailer class to:

a. Conveniently share default values throughout my codebase; and
b. Distinguish between development and production.

It looks like this:

<?php

/* MYMAILER
 * Our own custom mailer class. Makes it easier to use default settings for all things email.
 *
 * Rules of thumb:
 * - Keep bounce rate under 5%.
 * - Keep complaint rate under 0.1%
 *
 * Tips:
 * - To check that the SPF and DKIM settings are correct, simply send an email (e.g.: to Gmail) and check its headers.
 * - Check URIBL.com and SURBL.org to check my links are not blacklisted.
 *
 * Documentation:
 * - Best practices: http://media.amazonwebservices.com/AWS_Amazon_SES_Best_Practices.pdf -- includes iSP postmaster pages for many ISPs on last page.
 * - Header fields: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/header-fields.html
 * ================================================================================= */
class MyMailer extends PHPMailer {
    public function __construct() {
        parent::__construct();


        $this->CharSet = 'UTF-8';

        if ( defined('APP_DEVELOPMENT') ) {

            /**
             * Local development. e.g.: mhsendmail + MailHog.
             **/
            $this->IsMail(); /** PHPMailer's default, but let's be explicit here. **/

        } else {

            /**
             * Production, i.e.: Amazon's SES.
             * The `Username` and `Password` SMTP credentials are AWS `Access key ID`/`Secret access key` created specifically for Amazon SES.
             *  Cf. `AWS Console › SES › SMTP Settings` to create them.
             *  Cf. `AWS Console › IAM › Users` to list the access keys (passwords only available when creating credentials.)
             *      CAREFUL! Creating new access keys for the SMTP user via IAM is technically possible but it does NOT work!
             *      Instead, use the `SES › SMTP Settings` interface.
             * Remember that any FROM email has to be verified (either via domain or via email inbox.)
             **/
            $this->IsSMTP(true);
            $this->SMTPAuth = true;
            $this->SMTPSecure = "tls";

            $url = parse_url( getenv('SMTP_URL') );
            if ( $url == FALSE ) {
                $log->error( 'Failed to parse SMTP_URL' );
            }
            $this->Host= $url['host'];
            $this->Port = $url['port'];
            $this->Username = $url['user'];
            $this->Password = $url['pass'];

        }


        /**
         * Default values common to local + SES. Constant defined in config.php
         * Used defensively only.
         * Those values would and should be overwritten by the code.
         * Apparently, the order matters (TBC.)
         **/
        $this->AddReplyTo( MAIL_DEFAULT_ADDREPLYTO );
        $this->FromName = MAIL_DEFAULT_FROMNAME;
        $this->From = MAIL_DEFAULT_FROM;
    }
}

?>

I hope this helps.

Anil

The below entry helped me resolve the issue I was facing with non delivery of my emails. While I had all the other configurations done but could not understand on why it was not working (not a PHP developer and hence not easy to figure out).

$mail->SMTPSecure = 'tls';
Akash Ahire

Just increase your sending mail limit quota on AWS SES

I had the same issue with AWS ec2 and then by searching solution, I got that AWS SES send mails only if receivers is also verified if you are in amazon sandbox.

If you want to send mail to another mail id( that is not verified ) then you have to increase your sending limit quota. just follow the steps shown in URL

http://docs.aws.amazon.com/ses/latest/DeveloperGuide/submit-extended-access-request.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!