How to create RAW-Email-Message on AWS-SES in Swift 5

為{幸葍}努か 提交于 2021-01-29 18:50:59

问题


I want to create a RAW-Email inside my iOS-App. Looking to the documentation, I need to encode my message to MIME-Standard, but I'm not so familiar with this topic. In the documentation there is also example-code for python and java.

How can I achieve this in SWIFT?

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)
        }
    }
}

Sending an empty mail with my code works so far.


回答1:


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)
        }
    }
}

You can use it like this and can handle the result in the completionHandler.

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)
                    }

I hope it will help somebody in future :)



来源:https://stackoverflow.com/questions/58744770/how-to-create-raw-email-message-on-aws-ses-in-swift-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!