2

我正在尝试从 iOS 中的 SKPSMTP 代码发送 HTML 电子邮件。现在,我只是发送纯文本,但我正在尝试稍微升级一下。我在下面包含了该代码。

我找不到任何文档。如何上传 HTML 文件并将其作为正文包含在内。此外,还有一个图像从与 HTML 文件相同的目录中加载,如果这对答案有影响的话。谢谢。

 NSMutableString *emailBody = [NSMutableString stringWithFormat:@"Here's your code again, "];
                [emailBody appendString:userCode];
                SKPSMTPMessage *email = [[SKPSMTPMessage alloc] init];
                email.fromEmail = @"me@gmail.com";
                NSString *toEmail = [NSString stringWithFormat:@"%@", self.loginInput.text];
                email.toEmail = toEmail;
                email.relayHost = @"smtp.gmail.com";
                email.requiresAuth = YES;
                email.login = @"me@gmail.com";
                email.pass = @"myPass";
                email.subject = @"Your Validation Code";
                email.wantsSecure = YES;
                email.delegate = self;
                NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain",kSKPSMTPPartContentTypeKey,
                                           emailBody,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey, nil];
                email.parts = [NSArray arrayWithObjects:plainPart, nil];
                // Send it!
                [email send];
4

1 回答 1

3

所以,这是我遇到的答案,这样其他人都可以从我的挣扎中受益:

 //Send them an e-mail
NSError* error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource: @"loginEmail" ofType: @"html"];
NSString *result = [NSString stringWithContentsOfFile: path encoding:
                    NSUTF8StringEncoding error: &error];
NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:@"<!--INJECT CODE HERE -->"
                              options:0
                              error:&error];
NSString *emailBody = [regex stringByReplacingMatchesInString:result options:0 range:NSMakeRange(0, [result length]) withTemplate:code];
NSLog(@"%@", [emailBody class]);
SKPSMTPMessage *email = [[SKPSMTPMessage alloc] init];
email.fromEmail = @"myemail@gmail.com";
NSString *toEmail = [NSString stringWithFormat:@"%@", self.loginInput.text];
email.toEmail = toEmail;
email.relayHost = @"smtp.gmail.com";
email.requiresAuth = YES;
email.login = @"myemail@gmail.com";
email.pass = @"myPass"
email.subject = @"Your Validation Code";
email.wantsSecure = YES;
email.delegate = self;
NSDictionary *htmlPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/html",kSKPSMTPPartContentTypeKey,                    emailBody,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey, nil];
email.parts = [NSArray arrayWithObjects:htmlPart,  nil];
// Send it!
NSLog(@"ABOUT TO SEND");
[email send];

因此,我必须编写一个 HTML 文件,在 tinypic 上托管我的所有图像以包含在 HTML 中,编写一些文本以正则表达式切换我的代码变量,将其加载到此处并将其作为我的电子邮件的一部分附上密钥“文本/html”。此代码有效,但如果有人有任何其他有用的建议,我愿意将它们标记为正确答案!

于 2013-08-06T19:45:19.377 回答