Sample code for 30+ languages & platforms
Swift

Amazon SES v2: Attach a PDF to a simple email

See more Amazon SES Examples

Place invoice.pdf in the working directory and use a verified sender. RawContent contains the Base64 representation of the file bytes for JSON transport. ContentTransferEncoding tells SES how to encode the PDF when it assembles the outgoing MIME message. For custom MIME structure, see Send a raw MIME email.

SES attachments · SendEmail

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = false

    let pdf = CkoBinData()!
    success = pdf.loadFile(path: "invoice.pdf")
    if success != true {
        print("\(pdf.lastErrorText!)")
        return
    }

    if pdf.numBytes.intValue == 0 {
        print("invoice.pdf is empty.")
        return
    }

    let request = CkoJsonObject()!
    request.updateString(jsonPath: "FromEmailAddress", value: "sender@example.com")
    request.updateString(jsonPath: "Destination.ToAddresses[0]", value: "success@simulator.amazonses.com")
    request.updateString(jsonPath: "Content.Simple.Subject.Data", value: "Invoice 1042")
    request.updateString(jsonPath: "Content.Simple.Body.Text.Data", value: "Your invoice is attached.")
    request.updateString(jsonPath: "Content.Simple.Attachments[0].FileName", value: "invoice-1042.pdf")
    request.updateString(jsonPath: "Content.Simple.Attachments[0].ContentType", value: "application/pdf")
    request.updateString(jsonPath: "Content.Simple.Attachments[0].ContentDisposition", value: "ATTACHMENT")
    request.updateString(jsonPath: "Content.Simple.Attachments[0].ContentTransferEncoding", value: "BASE64")
    request.updateString(jsonPath: "Content.Simple.Attachments[0].RawContent", value: pdf.getEncoded(encoding: "base64"))
    //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    var region: String? = "us-west-2"
    let host = CkoStringBuilder()!
    host.append(value: "email.")
    host.append(value: region)
    host.append(value: ".amazonaws.com")

    let signer = CkoAuthAws()!
    //  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"

    let connection = CkoSocket()!
    connection.requireSslCertVerify = true
    success = connection.connect(hostname: host.getAsString(), port: 443, ssl: true, maxWaitMs: 10000)
    if success != true {
        print("\(connection.lastErrorText!)")
        return
    }

    let rest = CkoRest()!
    success = rest.useConnection(connection: connection, autoReconnect: false)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    success = rest.setAuthAws(authProvider: signer)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    //  For temporary AWS credentials, supply the session token from the same provider.
    var sessionToken: String? = ""
    if sessionToken != "" {
        rest.addQueryParam(name: "X-Amz-Security-Token", value: sessionToken)
    }

    rest.addHeader(name: "Content-Type", value: "application/json")
    var responseText: String? = rest.fullRequestString(httpVerb: "POST", uriPath: "/v2/email/outbound-emails", bodyText: request.emit())
    if rest.lastMethodSuccess != true {
        print("\(rest.lastErrorText!)")
        return
    }

    if rest.responseStatusCode.intValue != 200 {
        print("SES returned HTTP \(rest.responseStatusCode.intValue) \(responseText!)")
        return
    }

    let response = CkoJsonObject()!
    success = response.load(json: responseText)
    if success != true {
        print("\(response.lastErrorText!)")
        return
    }

    print("SES accepted the message: \(response.string(of: "MessageId")!)")

}