[英]iOS Can I inject HTML code in the emails send by mail app?
我们经营文具业务,为人们设计 email 文具、签名等。 在 PC 上使用这些没有问题,因为在大多数邮件客户端中很容易将 HTML 添加到 email 内容中。 但是,这对 iOS 可行吗?
对于移动设备,我们现在如何做到这一点是我们告诉用户添加我们的 SMTP 服务器并通过它发送消息。 这个 SMTP 的工作原理是它在发送前用 HTML 包装每个 email。 我想知道这是否可以直接在 iPhone 中完成,并在发送之前对电子邮件进行某种后处理?
AFAIK 您无法捕获从内置 email 客户端发送的电子邮件。 但是,您可以编写自己的应用程序,使用MFMailComposeViewController
使用 HTML 发送电子邮件。
编辑:添加了一些示例代码:
- (void) sendEventInEmail
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Email Subject"];
// Fill out the email body text
NSString *iTunesLink = @"http://itunes.apple.com/gb/app/whats-on-reading/id347859140?mt=8"; // Link to iTune App link
NSString *content = [eventDictionary objectForKey:@"Description"];
NSString *imageURL = [eventDictionary objectForKey:@"Image URL"];
NSString *findOutMoreURL = [eventDictionary objectForKey:@"Link"];
NSString *emailBody = [NSString stringWithFormat:@"<br /> <a href = '%@'> <img src='%@' align=left style='margin:5px' /> </a> <b>%@</b> <br /><br />%@ <br /><br />Sent using <a href = '%@'>What's On Reading</a> for the iPhone.</p>", findOutMoreURL, imageURL, emailSubject, content, iTunesLink];
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
它应该看起来像这样:
// Compose body
NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:@"<html><body>"] autorelease];
[emailBody appendString:@"<p>Hi<br><br>THis is some HTML</p>"];
[emailBody appendString:@"</body></html>"];
// Compose dialog
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setToRecipients:[NSArray arrayWithObject:@"test@test.com"]];
// Set subject
[emailDialog setSubject:@"I got a question!"];
// Set body
[emailDialog setMessageBody:emailBody isHTML:YES];
// Show mail
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
如上所述,不要忘记委托 8)
HTH 马库斯
使用 MFMailComposeViewController,您可以在 email 的主体中包含 HTML。 此外,您可以附加图像。 请参阅这些方法:
- (void)setMessageBody:(NSString*)body isHTML:(BOOL)isHTML
- (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename
您可以获取他们的 email 的正文并更改它(通过使用stringWithFormat:
甚至是 NSMutableString)以使其成为 html。 使用 MFMailComposeViewController 的-(void)setMessageBody:(NSString*)body isHTML:(BOOL)isHTML
例子:
NSString *bodyText = [[NSString alloc] initWithFormat:@"<html><p>%@</p></html", bodyField.text]; //Not an HTML expert
然后发送 email,正文为 bodyText。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.