Wednesday, March 26, 2014

Get SMTP credentials from IAM access keys for Amaon Simple Email Service.

Here's some code to get you Amazon SES SMTP credentials from your access key id and secret.

def get_smtp_creds(access_key_id, secret_access_key):
    message = 'SendRawEmail'
    version = 0x02

    sig = hmac.new(
        secret_access_key,
        msg=message,
        digestmod=hashlib.sha256)
    sig_bytes = sig.digest()
    sig_and_version_bytes = (struct.pack('B', version) + sig_bytes)
    smtp_password = base64.b64encode(sig_and_version_bytes)

    return access_key_id, smtp_password