使用Mailgun发送带附件的电子邮件

时间:2015-05-12 00:02:14

标签: objective-c afnetworking-2 mailgun

我正在将Mailgun集成到我的iOS应用程序中,并且我正在尝试发送带有附件的电子邮件。电子邮件将被发送,但附件似乎被忽略。有任何想法吗?代码如下。我正在使用AFNetworking 2,而且我没有使用本机Mailgun Objective-C SDK,因为它似乎没有被维护。

NSString *path = [NSString stringWithFormat:@"https://api:%@@api.mailgun.net/v3/%@/messages", kTIXMailgunAPIKey, kTIXMailgunDomain];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[ @"text/plain", @"text/html", @"application/json" ] ];

NSData *attachment = [attachments firstObject];

NSDictionary *parameters = @{
                             @"from"            : fromAddress,
                             @"to"              : toAddress,
                             @"subject"         : @"Inline",
                             @"text"            : @"Email body",
                             @"content-type"    : @"multipart/form-data",
                             @"attachment"      : attachment,
                             };
[manager POST:path
   parameters:parameters
      success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSLog(@"Success sending message. Response: %@", responseObject);
 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     NSLog(@"Error sending message: %@", error);
 }];

我可以使用curl发送附件,例如:

curl -s "https://api:key-
[redacted]@api.mailgun.net/v3/sandbox[redacted].mailgun.org/messages" \
-F from=[redacted]@gmail.com \
-F to=[redacted]@gmail.com \
-F subject='Check this out!' \
-F text='LOL' \
-F attachment=@"lolcat.png"

1 个答案:

答案 0 :(得分:0)

现在可以使用邮件枪从iOS应用程序中发送附件:

Mailgun *mailgun = [Mailgun clientWithDomain:@"yourdomain.com" apiKey:@"key"];

MGMessage *message = [MGMessage messageFrom:@"Someone <your.address@yourdomain.com>"
                                         to:@"Someone else <someone@else.com>"
                                    subject:@"Hello"
                                       body:@"This is a test email."];

[message addAttachment:[NSData dataWithContentsOfFile:@"filePath"] withName:@"fileName" type:@"fileType"];

[mailgun sendMessage:message success:^(NSString *messageId) {
    NSLog(@"Message %@ sent successfully!", messageId);
} failure:^(NSError *error) {
    NSLog(@"Error sending message. The error was: %@", [error userInfo]);
}];