2012-02-11 160 views
0

我有一個由文本字段(5個字段)組成的聯繫表單,我希望通過電子郵件發送到一個電子郵件地址。我如何在xCode中執行此操作?發送電子郵件中的聯繫人表單字段

+1

前[SO](http://stackoverflow.com/questions/7087199/xcode已回答-4-ios-send-an-email-using-smtp-from-inside-my-app) – 2012-02-11 00:33:45

回答

0

鏈接的帖子有類似的答案,但我添加了我的代碼,因爲它已經檢查了canSendMail。我還留下了一堆評論代碼,可以很容易地在電子郵件中添加其他內容。

注意,這基本上是比較容易,如果你只指定的iOS 5

我有一個免費的應用程序,QCount,使用此代碼。事實上,我希望我的一切剝離定製從我的複製和粘貼:-) http://itunes.apple.com/ng/app/qcount/id480084223?mt=8

享受,

達明

在你的.h:

#import <MessageUI/MessageUI.h> 

方法在你的。米:

- (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:@"[email protected]", nil]; 
// NSArray *ccRecipients = [[NSArray alloc] init]; 
// NSArray *bccRecipients = [[NSArray alloc] init]; 
// NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
// NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", 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:[email protected][email protected],[email protected]&subject=Hello from California!"; 
NSString *body = @"&body=Feedback"; 

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

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

對於任何人在這個問題上磕磕絆絆,你可以使用thi的iOS聯繫表格。

這適合我的需求,它使用PHP組件來實際發送電子郵件。 (包含在示例項目的示例腳本

我把它貼到這裏Github上:

https://github.com/mikecheckDev/MDContactForm

+0

非常感謝你分享這個,很棒!:-) – kernix 2012-11-24 21:01:39

相關問題