Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL cDomainName
LOCAL nCheckExisting
LOCAL cRegion
LOCAL oHost
LOCAL oSigner
LOCAL oConnection
LOCAL oRest
LOCAL cSessionToken
LOCAL cResponseText
LOCAL oPath
LOCAL oIdentity
LOCAL oResponse
LOCAL nCount
LOCAL cZone
LOCAL i
LOCAL cToken
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0
cDomainName := "example.com"
nCheckExisting := 0
// Use the same AWS Region for the identity, endpoint, and Signature Version 4.
cRegion := "us-west-2"
oHost := CreateObject("Chilkat.StringBuilder")
oHost:Append("email.")
oHost:Append(cRegion)
oHost:Append(".amazonaws.com")
oSigner := CreateObject("Chilkat.AuthAws")
// Supply the credentials from your application's credential provider.
oSigner:AccessKey := "AWS_ACCESS_KEY_ID"
oSigner:SecretKey := "AWS_SECRET_ACCESS_KEY"
oSigner:Region := cRegion
oSigner:ServiceName := "ses"
oConnection := CreateObject("Chilkat.Socket")
oConnection:RequireSslCertVerify := 1
nSuccess := oConnection:Connect(oHost:GetAsString(), 443, 1, 10000)
IF (nSuccess != 1)
? oConnection:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
RETURN
ENDIF
oRest := CreateObject("Chilkat.Rest")
nSuccess := oRest:UseConnection(oConnection, 0)
IF (nSuccess != 1)
? oRest:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
RETURN
ENDIF
nSuccess := oRest:SetAuthAws(oSigner)
IF (nSuccess != 1)
? oRest:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
RETURN
ENDIF
// For temporary AWS credentials, supply the session token from the same provider.
cSessionToken := ""
IF (cSessionToken != "")
oRest:AddQueryParam("X-Amz-Security-Token", cSessionToken)
ENDIF
cResponseText := ""
IF (nCheckExisting == 1)
oPath := CreateObject("Chilkat.StringBuilder")
oPath:Append("/v2/email/identities/")
oPath:Append(cDomainName)
cResponseText := oRest:FullRequestNoBody("GET", oPath:GetAsString())
ELSE
oIdentity := CreateObject("Chilkat.JsonObject")
oIdentity:UpdateString("EmailIdentity", cDomainName)
oRest:AddHeader("Content-Type", "application/json")
cResponseText := oRest:FullRequestString("POST", "/v2/email/identities", oIdentity:Emit())
ENDIF
IF (oRest:LastMethodSuccess != 1)
? oRest:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oPath:destroy()
oIdentity:destroy()
RETURN
ENDIF
IF (oRest:ResponseStatusCode != 200)
? "SES returned HTTP " + Str(oRest:ResponseStatusCode) + " " + cResponseText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oPath:destroy()
oIdentity:destroy()
RETURN
ENDIF
oResponse := CreateObject("Chilkat.JsonObject")
nSuccess := oResponse:Load(cResponseText)
IF (nSuccess != 1)
? oResponse:LastErrorText
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oPath:destroy()
oIdentity:destroy()
oResponse:destroy()
RETURN
ENDIF
? "DKIM status: " + oResponse:StringOf("DkimAttributes.Status")
? "Verified for sending: " + Str(oResponse:BoolOf("VerifiedForSendingStatus"))
nCount := oResponse:SizeOfArray("DkimAttributes.Tokens")
IF (nCount < 1)
? "No Easy DKIM tokens were returned. Check this identity's DKIM configuration."
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oPath:destroy()
oIdentity:destroy()
oResponse:destroy()
RETURN
ENDIF
IF (oResponse:HasMember("DkimAttributes.SigningHostedZone") != 1)
? "Find the CNAME targets for this identity in the SES console."
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oPath:destroy()
oIdentity:destroy()
oResponse:destroy()
RETURN
ENDIF
cZone := oResponse:StringOf("DkimAttributes.SigningHostedZone")
i := 0
DO WHILE (i < nCount)
oResponse:I := i
cToken := oResponse:StringOf("DkimAttributes.Tokens[i]")
? cToken + "._domainkey." + cDomainName + " CNAME " + cToken + "." + cZone
i := i + 1
ENDDO
oHost:destroy()
oSigner:destroy()
oConnection:destroy()
oRest:destroy()
oPath:destroy()
oIdentity:destroy()
oResponse:destroy()