Compose mail in the iPhone app

24,615

Solution 1

You have to link against the MessageUI framework and use the class MFMailComposeViewController. Don't forget to import the framework (#import <MessageUI/MessageUI.h>).

The documentation with sample code: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html

Solution 2

Here's the code:

Obj-C:

(Don't forget to add the messageUI framework to your project!!!)

First import the message library:

#import <MessageUI/MessageUI.h>

Then mark your self as a delegate like this:

@interface MYViewController () <MFMailComposeViewControllerDelegate>

Then to pull up the composer (if user has email set up on their device):

- (IBAction)emailButtonPressed:(id)sender {
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] initWithNibName:nil bundle:nil];
        [composeViewController setMailComposeDelegate:self];
        [composeViewController setToRecipients:@[@"[email protected]"]];
        [composeViewController setSubject:@"example subject"];
        [self presentViewController:composeViewController animated:YES completion:nil];
    }
}

Then to handle the delegate callback and dismiss the composer:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    //Add an alert in case of failure
    [self dismissViewControllerAnimated:YES completion:nil];
}

SWIFT 3:

Import the relevant library:

import MessageUI

Mark your view controller as a delegate like so:

class MyViewController: UIViewController, MFMailComposeViewControllerDelegate {

Pull up composer (if user has email set up on their device):

@IBAction func emailButtonAction(_ sender: UIButton) {
        
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients(["[email protected]"])
            mail.setSubject("Example Subject")
            mail.setMessageBody("<p>Test</p>", isHTML: true)
            present(mail, animated: true)
        }
    }

Handle delegate callback and dismiss the composer:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true)
}

Solution 3

Here is the code to open Mail Composer in iOS:

Source:http://sickprogrammersarea.blogspot.in/2014/03/open-mail-composer-programmatically-in.html

#import "ViewController.h"
@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(send:) forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Send Mail" forState:UIControlStateNormal];
    button.frame = CGRectMake(100.0, 350.0, 100.0, 40.0);
    [self.view addSubview:button];

}
- (void) send:(id)sender{
    MFMailComposeViewController *comp=[[MFMailComposeViewController alloc]init];
    [comp setMailComposeDelegate:self];
    if([MFMailComposeViewController canSendMail])
    {
        [comp setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
        [comp setSubject:@"From my app"];
        [comp setMessageBody:@"Hello bro" isHTML:NO];
        [comp setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentViewController:comp animated:YES completion:nil];
    }
    else{
        UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
        [alrt show];

    }
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    if(error)
    {
        UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
        [alrt show];
        [self dismissModalViewControllerAnimated:YES];
    }
    else{
        [self dismissModalViewControllerAnimated:YES];
    }

}

Solution 4

Lots of them. You can start here and here. And check stackoverflow right here.

Share:
24,615

Related videos on Youtube

K.Honda
Author by

K.Honda

Updated on September 22, 2021

Comments

  • K.Honda
    K.Honda over 2 years

    In my app, I have an abouts page. I would like to place a round rect button which when I press it, I would be able to send an email to the company with an embedded email address.

    Are there any tutorials to do this?

    Thanks in advance.

  • Fonix
    Fonix almost 10 years
    i seem to get a crash when creating the composerVC in iOS 6 *** Assertion failure in NSDictionary *_UIRecordArgumentOfInvocationAtIndex(NSInvocation *, NSUInteger, BOOL)(), /SourceCache/UIKit_Sim/UIKit-2380.17/UIAppearance.m:1118 i fixed it using this answer
  • Matjan
    Matjan over 9 years
    That issue has to do with UIAppearance, the above is still the correct way to implement sending email.
  • Matrosov Oleksandr
    Matrosov Oleksandr about 9 years
    I am not sure if we should use here self [dismissModalViewControllerAnimated:YES]; instead I think we should use [controller dismissModalViewControllerAnimated:YES];