Send RAW-EMail in iOS-App with SWIFT

0

I want to create a RAW-Email inside my iOS-App. Looking to the documentation, I need to encode my message respectively the message body to MIME-Standard, but I'm not so familiar with this topic. In the documentation https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-raw.html#send-email-mime-encoding there is also example-code for python and java, but how can I achieve this in SWIFT? How can I create in SWIFT this MIME-Data?

func sendRawMail(){
    let sender = "sender@mail.com"
    let recipient = "recipient@mail.com"

    let rawMessage = AWSSESRawMessage()
   // rawMessage?.data = "I guess HERE I have to put the MIME-   Data?!"
    let rawRequest = AWSSESSendRawEmailRequest()
    rawRequest?.destinations = [recipient]
    rawRequest?.source = sender

    rawRequest?.rawMessage = rawMessage

    AWSSES.default().sendRawEmail(rawRequest!) { (response, error) in
        if let response = response{
            print(response)
        }
        if let error = error{
            print(error)
        }
    }
}

With my function, sending an empty email works.
Thank you
Chris

asked 4 years ago445 views
2 Answers
0

After a lot of reading, I finally get it working. You need to write a big string, based on MIME-Standards. This big string you have to encode to BASE64. Attachments needed also to transform from data to an BASE64-String.

I will post my code, which can send Mails with an .png image. I will test other fileTypes as well, but it have to be the same principle.

First I created a enum for my fileTypes.

enum DataTypes{
case png
case jpg
case pdf
}

Now I created a function to get the string-value for the specific DataType

func getMIMEDataType(dataType:DataTypes) -> String{
    var MIMEData = String()
    switch dataType {
    case .png:
        MIMEData = "image/png"
    case .jpg:
        MIMEData = "image/jpg"
    case .pdf:
        MIMEData = "application/pdf"
    }
    return MIMEData
}

Finally the function to send the raw-mail. There are already variables in the message-string so you can use this function flexible.

func sendRawMail(sender:String,reciepients:[String],subject:String,message:String,attachment:Data?,dataType:DataTypes?,attachmentName:String?,completion: @escaping (_ messageCode:String?,_ error:Error?) -> ()){

    let attachmentString = attachment!.base64EncodedString(options: .lineLength64Characters)

    let MIMEDataType = getMIMEDataType(dataType: dataType!)

    let message:String = """
    Subject: \(subject)
    MIME-Version: 1.0
    Content-Type: multipart/mixed;
            boundary="XXXXboundary text"

    This is a multipart message in MIME format.

    --XXXXboundary text
    Content-Type: text/plain

    \(message)

    --XXXXboundary text
    Content-Type: \(MIMEDataType);
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment; filename="\(attachmentName!).\(MIMEDataType.components(separatedBy: "/")[1])"

    \(attachmentString)

    --XXXXboundary text--
    """

    let data = message.data(using: .utf8)
    let rawMessage = AWSSESRawMessage()
    rawMessage?.data = data
    let rawRequest = AWSSESSendRawEmailRequest()
    rawRequest?.destinations = reciepients
    rawRequest?.source = sender

    rawRequest?.rawMessage = rawMessage

    AWSSES.default().sendRawEmail(rawRequest!) { (response, error) in
        if let response = response{
            completion(response.messageId,nil)
        }
        if let error = error{
            completion(nil,error)
        }
    }
}

usage:

sendRawMail(sender: "sender.mail@mail.com", reciepients: ["recipient.mail@mail.com"], subject: "This is a test", message: "TestMessage", attachment: attachment.data, dataType: .png, attachmentName: "testpicture") { (messageID, error) in
                    if let messageID = messageID{
                        print(messageID)
                    }
                    if let error = error{
                        print(error)
                    }

hope it will help somebody in future :)

answered 4 years ago
0

Hi ChrisSTMS,

I'm glad you were able to get it working. I was just researching this myself, and not having much luck. I definitely appreciate you posting the solution for other users to find. Thank you!

Brent @ AWS

AWS
answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions