Sample code for 30+ languages & platforms
Python

Amazon SES v2: Verify a sending domain with Easy DKIM

See more Amazon SES Examples

Use a domain you control. Run once to create the regional identity, publish the returned CNAMEs with your DNS provider, then set checkExisting to true and run again to read the status. The target suffix comes from SES. If your DNS provider appends the domain automatically, enter just the relative record name.

CreateEmailIdentity · GetEmailIdentity · Easy DKIM records

Chilkat Python Downloads

Python
import sys
import chilkat2

success = False

# Assumes Chilkat is unlocked. See Global Unlock Sample.
success = False

domainName = "example.com"
checkExisting = False
# Use the same AWS Region for the identity, endpoint, and Signature Version 4.
region = "us-west-2"
host = chilkat2.StringBuilder()
host.Append("email.")
host.Append(region)
host.Append(".amazonaws.com")

signer = chilkat2.AuthAws()
# Supply the credentials from your application's credential provider.
signer.AccessKey = "AWS_ACCESS_KEY_ID"
signer.SecretKey = "AWS_SECRET_ACCESS_KEY"
signer.Region = region
signer.ServiceName = "ses"

connection = chilkat2.Socket()
connection.RequireSslCertVerify = True
success = connection.Connect(host.GetAsString(),443,True,10000)
if (success != True):
    print(connection.LastErrorText)
    sys.exit()

rest = chilkat2.Rest()
success = rest.UseConnection(connection,False)
if (success != True):
    print(rest.LastErrorText)
    sys.exit()

success = rest.SetAuthAws(signer)
if (success != True):
    print(rest.LastErrorText)
    sys.exit()

# For temporary AWS credentials, supply the session token from the same provider.
sessionToken = ""
if (sessionToken != ""):
    rest.AddQueryParam("X-Amz-Security-Token",sessionToken)

responseText = ""
if (checkExisting == True):
    path = chilkat2.StringBuilder()
    path.Append("/v2/email/identities/")
    path.Append(domainName)
    responseText = rest.FullRequestNoBody("GET",path.GetAsString())
else:
    identity = chilkat2.JsonObject()
    identity.UpdateString("EmailIdentity",domainName)
    rest.AddHeader("Content-Type","application/json")
    responseText = rest.FullRequestString("POST","/v2/email/identities",identity.Emit())

if (rest.LastMethodSuccess != True):
    print(rest.LastErrorText)
    sys.exit()

if (rest.ResponseStatusCode != 200):
    print("SES returned HTTP " + str(rest.ResponseStatusCode) + " " + responseText)
    sys.exit()

response = chilkat2.JsonObject()
success = response.Load(responseText)
if (success != True):
    print(response.LastErrorText)
    sys.exit()

print("DKIM status: " + response.StringOf("DkimAttributes.Status"))
print("Verified for sending: " + str(response.BoolOf("VerifiedForSendingStatus")))
count = response.SizeOfArray("DkimAttributes.Tokens")
if (count < 1):
    print("No Easy DKIM tokens were returned. Check this identity's DKIM configuration.")
    sys.exit()

if (response.HasMember("DkimAttributes.SigningHostedZone") != True):
    print("Find the CNAME targets for this identity in the SES console.")
    sys.exit()

zone = response.StringOf("DkimAttributes.SigningHostedZone")
i = 0
while (i < count) :
    response.I = i
    token = response.StringOf("DkimAttributes.Tokens[i]")
    print(token + "._domainkey." + domainName + " CNAME " + token + "." + zone)
    i = i + 1