Tuesday 8 September 2015

AWS SES with bash curl

#!/bin/bash

TO="Email To <to@example.com>"
FROM="Email From <from@example.com>"
SUBJECT="<YOUR SUBJECT HERE>"
MESSAGE="<YOUR MESSAGE HERE>"

date="$(date -R)"
priv_key="$AWS_SECRET_KEY"
access_key="$AWS_ACCESS_KEY"
signature="$(echo -n "$date" | openssl dgst -sha256 -hmac "$priv_key" -binary | base64 -w 0)"
auth_header="X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=$access_key, Algorithm=HmacSHA256, Signature=$signature"
endpoint="https://email.us-east-1.amazonaws.com/"

action="Action=SendEmail"
source="Source=$FROM"
to="Destination.ToAddresses.member.1=$TO"
subject="Message.Subject.Data=$SUBJECT"
message="Message.Body.Text.Data=$MESSAGE"

curl -v -X POST -H "Date: $date" -H "$auth_header" --data-urlencode "$message" --data-urlencode "$to" --data-urlencode "$source" --data-urlencode "$action" --data-urlencode "$subject"  "$endpoint"
How to configure sendmail/postfix with SES:
http://blog.celingest.com/en/2013/02/13/integrating-ses-and-amazon-linux/
First we save the original configuration files:
sudo cp -p -f /etc/mail/sendmail.mc /etc/mail/sendmail.mc.bak
sudo cp -p -f /etc/mail/sendmail.cf /etc/mail/sendmail.cf.bak
Then we create a file with the authentication tokens substituting IAM_USERNAME and IAM_PASSWORD with the IAM credentials relative to SES – SMTP:
cat << END | sudo tee /etc/mail/authinfo
AuthInfo:email-smtp.us-east-1.amazonaws.com "U:root" "I:IAM_USERNAME" "P:IAM_PASSWORD" "M:LOGIN"
AuthInfo:ses-smtp-prod-335357831.us-east-1.elb.amazonaws.com "U:root" "I:IAM_USERNAME" "P:IAM_PASSWORD" "M:LOGIN"
END
 
cat << END | sudo tee /etc/mail/access
Connect:email-smtp.us-east-1.amazonaws.com RELAY
Connect:ses-smtp-prod-335357831.us-east-1.elb.amazonaws.com RELAY
END
In /etc/mail/sendmail.mc we’ll add the following lines  ensuring that the “FEATURE” lines are always above the ones starting with “MAILER” and changing the YOUR_DOMAIN for the domain authorized in SES:
define(`SMART_HOST', `email-smtp.us-east-1.amazonaws.com')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 25')dnl
define(`confAUTH_MECHANISMS', `LOGIN PLAIN')dnl
FEATURE(`authinfo', `hash -o /etc/mail/authinfo.db')dnl
MASQUERADE_AS(YOUR_DOMAIN)dnl
FEATURE(masquerade_envelope)dnl
FEATURE(masquerade_entire_domain)dnl
Finally we can install the sendmail-cf packet, not included by default applying the changes:
sudo yum install sendmail-cf -y
( cd /etc/mail ; sudo make )
sudo service sendmail restart

No comments:

Post a Comment