0

I have a contact form made of text fields (5 fields) that I would like to send via email to a single email address. How do I do this in xCode?

  • 1
    Already answered before [SO](http://stackoverflow.com/questions/7087199/xcode-4-ios-send-an-email-using-smtp-from-inside-my-app) – Joel Kravets Feb 11 '12 at 00:33

2 Answers2

1

For anyone stumbling across this question, you can use this drop-in iOS contact form.

This fit my needs well, it uses a PHP component to actually send the email. (an example script is included in the sample project.

I posted it to Github here:

https://github.com/mikecheckDev/MDContactForm

MikecheckDev
  • 2,176
  • 2
  • 15
  • 6
0

The linked post has a similar answer, but I'm adding my code since it checks for canSendMail already. I also left in a bunch of commented code that makes it easy to add other stuff to the email.

Note that this is substantially easier if you are only targeting iOS 5.

I have a free app, QCount, that uses this code. Indeed, I hope I stripped everything custom from my copy-and-paste :-) http://itunes.apple.com/ng/app/qcount/id480084223?mt=8

Enjoy,

Damien

In your .h:

#import <MessageUI/MessageUI.h>

Methods in your .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]];
}
Damien Del Russo
  • 1,040
  • 8
  • 19