Tag: amazon ses

  • Configure Postfix to use Amazon SES

    Install postfix with

    apt-get update && apt-get install postfix libsasl2-modules -y
    

    Update postfix config with

    sed -i "s/default_transport = error/# default_transport = error/g" /etc/postfix/main.cf
    sed -i "s/relay_transport = error/# relay_transport = error/g" /etc/postfix/main.cf
    sed -i "s/relayhost =/# relayhost =/g" /etc/postfix/main.cf
    

    Edit

    vi /etc/postfix/main.cf
    

    Add to end of the file

    relayhost = [SMTP_SERVER_NAME]:587
    smtp_tls_security_level = encrypt
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = noanonymous
    

    In the above, replace SMTP_SERVER_NAME with your Amazon SES mail server, for example “email-smtp.us-east-1.amazonaws.com”, you can see it in your Amazon SES console.

    Run

    echo "[SMTP_SERVER_NAME]:587 SMTP_USERNAME:SMTP_PASSWORD" > /etc/postfix/sasl_passwd
    

    Replace SMTP_SERVER_NAME, SMTP_USERNAME and SMTP_PASSWORD with your actual credentials.

    Now run

    postmap /etc/postfix/sasl_passwd
    

    Restart postfix

    service postfix restart
    

    You can test mail working with

    echo "test" | mail -r you@SES_VERIFIED_DOMAIN -s "test" [email protected]
    

    For sending from Apache, you may need to set myorigin in postfix confgiration with your veirfied domain. This can be done by editing

    vi /etc/mailname
    

    You can verify myorgin with

    root@ip-172-31-30-228:~# postconf | grep myorigin
    append_at_myorigin = yes
    myorigin = /etc/mailname
    root@ip-172-31-30-228:~#
    

    Sending Mail from Amazon EC2

    When sending email from Amazon EC2, it rejected with

    Aug 28 17:20:59 ip-172-31-11-238 postfix/smtp[27260]: 1BB10831A0: to=, relay=email-smtp.us-east-1.amazonaws.com[23.23.196.20]:587, delay=2.9, delays=0.02/0.03/1.7/1.2, dsn=5.0.0, status=bounced (host email-smtp.us-east-1.amazonaws.com[23.23.196.20] said: 554 Message rejected: Email address is not verified. The following identities failed the check in region US-EAST-1: [email protected] (in reply to end of DATA command))
    

    Problem is solved by editing

    vi /etc/postfix/main.cf
    

    Find

    myhostname = ip-172-31-28-58.ap-southeast-2.compute.internal
    

    Replace with

    myhostname = AWS_VERIFIED_DOMAIN_HERE
    

    Amazon SES

  • Sending Emails from PHP using Amazon SES

    To create a PHP script to sent email using Amazon SES, create a folder

    mkdir ses-test
    cd ses-test
    

    Install phpMailer using composer

    composer require phpmailer/phpmailer
    

    Create a PHP file with following content

    SMTPDebug = 2;
        $mail->isSMTP();
        $mail->Host = 'email-smtp.us-east-1.amazonaws.com';  // YOUR SES END POINT
        $mail->SMTPAuth = true;
        $mail->Username = 'AKIAJSKJUHBOU2ARXVLQ';                 // SMTP Username
        $mail->Password = 'AjMmd3gRkmTyCY2QoCDkWmkGKA345zuhseS7gV0X6uz0';                           // SMTP Password
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
    
        //Recipients
        $mail->setFrom('info@YOUR_SES_VERIFIED_DOMAIN.com', 'YOUR_SES_VERIFIED_DOMAIN.com');
        $mail->addAddress('[email protected]', 'Your Name');     // If your SES is in Sandbox, you need to verify your email. 
        $mail->addReplyTo('info@YOUR_SES_VERIFIED_DOMAIN.com', 'YOUR_SES_VERIFIED_DOMAIN.com');
    
    
        //Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body in bold!';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }
    

    Now test it by running

    root@ok:~/ses-test# php 1.php
    2018-01-15 08:45:54 SERVER -> CLIENT: 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-2762711029 fDkTVziQIcNhSCkxwSpA
    2018-01-15 08:45:54 CLIENT -> SERVER: EHLO ok
    2018-01-15 08:45:54 SERVER -> CLIENT: 250-email-smtp.amazonaws.com
                                          250-8BITMIME
                                          250-SIZE 10485760
                                          250-STARTTLS
                                          250-AUTH PLAIN LOGIN
                                          250 Ok
    2018-01-15 08:45:54 CLIENT -> SERVER: STARTTLS
    2018-01-15 08:45:54 SERVER -> CLIENT: 220 Ready to start TLS
    2018-01-15 08:45:54 CLIENT -> SERVER: EHLO ok
    2018-01-15 08:45:54 SERVER -> CLIENT: 250-email-smtp.amazonaws.com
                                          250-8BITMIME
                                          250-SIZE 10485760
                                          250-STARTTLS
                                          250-AUTH PLAIN LOGIN
                                          250 Ok
    2018-01-15 08:45:54 CLIENT -> SERVER: AUTH LOGIN
    2018-01-15 08:45:54 SERVER -> CLIENT: 334 VXNlcm5hbWU6
    2018-01-15 08:45:54 CLIENT -> SERVER: