发送电子邮件中的联系表单字段

时间:2012-02-11 00:28:11

标签: ios xcode messageui

我有一个由文本字段(5个字段)组成的联系表单,我希望通过电子邮件将其发送到一个电子邮件地址。我如何在xCode中执行此操作?

2 个答案:

答案 0 :(得分:1)

对于遇到此问题的任何人,您可以使用此插入式iOS联系表单。

这很符合我的需求,它使用PHP组件来实际发送电子邮件。 (示例项目中包含示例脚本。

我把它发布到Github:

https://github.com/mikecheckDev/MDContactForm

答案 1 :(得分:0)

链接的帖子有类似的答案,但我添加了我的代码,因为它已经检查了canSendMail。我还留下了一堆注释代码,可以很容易地将其他内容添加到电子邮件中。

请注意,如果您只定位iOS 5,这会非常容易。

我有一个使用此代码的免费应用程序QCount。实际上,我希望我从我的复制粘贴中删除了所有自定义内容:-) http://itunes.apple.com/ng/app/qcount/id480084223?mt=8

享受,

达明

在你的.h:

#import <MessageUI/MessageUI.h>

.m中的方法:

- (void)emailLabelPressed { // or whatever invokes your email
// Create a mail message in the user's preferred mail client
// by opening a mailto URL.  The extended mailto URL format
// is documented by RFC 2368 and is supported by Mail.app
// and other modern mail clients.
//
// This routine's prototype makes it easy to connect it as
// the action of a user interface object in Interface Builder.
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil)
{
    // We must always check whether the current device is configured for sending emails
    if ([mailClass canSendMail])
    {
        [self displayComposerSheet];
    }
    else
    {
        [self launchMailAppOnDevice];
    }
}
else
{
    [self launchMailAppOnDevice];
}
}

-(void)displayComposerSheet {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Your Form Subject"];

// Take screenshot and attach (optional, obv.)
UIImage *aScreenshot = [self screenshot];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(aScreenshot)];
[picker addAttachmentData:imageData mimeType:@"image/png" fileName:@"screenshot"];

// Set up the recipients.
NSArray *toRecipients = [NSArray arrayWithObjects:@"first@example.com", nil];
//    NSArray *ccRecipients = [[NSArray alloc] init];
//    NSArray *bccRecipients = [[NSArray alloc] init];
//    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
//  NSArray *bccRecipients = [NSArray arrayWithObjects:@"fourth@example.com", nil];

[picker setToRecipients:toRecipients];
//    [picker setCcRecipients:ccRecipients];
//    [picker setBccRecipients:bccRecipients];

// Attach an image to the email.
/*    NSString *path = [[NSBundle mainBundle] pathForResource:@"ipodnano"
 ofType:@"png"];
 NSData *myData = [NSData dataWithContentsOfFile:path];
 [picker addAttachmentData:myData mimeType:@"image/png"
 fileName:@"ipodnano"];
 */ 
// Fill out the email body text.
//    NSString *emailBody = @"Use this for fixed content.";

NSMutableString *emailBody = [[NSMutableString alloc] init];
[emailBody setString: @"Feedback"];
// programmatically add your 5 fields of content here.

[picker setMessageBody:emailBody isHTML:NO];
// Present the mail composition interface.
if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
    [self presentViewController:picker animated:YES completion:nil];
}  else {
    [self presentModalViewController:picker animated:YES];
}
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError *)error {
if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
    [self dismissViewControllerAnimated:YES completion:nil];
}  else {
    [self dismissModalViewControllerAnimated:YES];
}
}

-(void)launchMailAppOnDevice {
NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
NSString *body = @"&body=Feedback";

NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}