如何在Swift 5中在AWS-SES上创建RAW-Email-Message [英] How to create RAW-Email-Message on AWS-SES in Swift 5
问题描述
我想在iOS应用程序中创建RAW电子邮件.查看文档,我需要将消息编码为MIME-Standard,但是我对该主题不太熟悉.在文档中,还有用于python和java的示例代码.
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.
如何在SWIFT中实现这一目标?
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.
推荐答案
经过大量阅读,我终于使它工作了.您需要根据MIME标准写一个大字符串.您必须将此大字符串编码为BASE64.从数据到BASE64字符串的转换也需要附件.
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.
我将发布我的代码,该代码可以发送带有.png图像的邮件.我还将测试其他文件类型,但是必须具有相同的原理.
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.
首先,我为我的fileTypes创建了一个枚举.
First I created a enum for my fileTypes.
enum DataTypes{
case png
case jpg
case pdf
}
现在,我创建了一个函数来获取特定DataType的字符串值
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)
}
}
}
您可以像这样使用它,并可以在completionHandler中处理结果.
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 :)
这篇关于如何在Swift 5中在AWS-SES上创建RAW-Email-Message的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!