【问题标题】:Send email with attachments with Mailgun使用 Mailgun 发送带有附件的电子邮件
【发布时间】:2015-05-12 00:02:14
【问题描述】:

我正在将 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"

【问题讨论】:

    标签: objective-c afnetworking-2 mailgun


    【解决方案1】:

    现在可以使用邮件枪从您的 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]);
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 2015-01-13
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      相关资源
      最近更新 更多