17

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.

jquery404
  • 653
  • 1
  • 12
  • 26
K.Honda
  • 3,106
  • 5
  • 26
  • 37

4 Answers4

64

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:@[@"example@email.com"]];
        [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(["example@gmail.com"])
            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)
}
Matjan
  • 3,591
  • 1
  • 33
  • 31
  • 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](http://stackoverflow.com/a/19867457/1219956) – Fonix Jun 03 '14 at 09:00
  • That issue has to do with UIAppearance, the above is still the correct way to implement sending email. – Matjan Nov 02 '14 at 23:10
13

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

mattsven
  • 22,305
  • 11
  • 68
  • 104
martiall
  • 981
  • 6
  • 7
4

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:@"imstkgp@gnail.com", 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];
    }

}
Stunner
  • 12,025
  • 12
  • 86
  • 145
Santosh
  • 1,254
  • 2
  • 16
  • 31
  • I am not sure if we should use here self [dismissModalViewControllerAnimated:YES]; instead I think we should use [controller dismissModalViewControllerAnimated:YES]; – Matrosov Oleksandr Apr 29 '15 at 19:27
3

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

Community
  • 1
  • 1
Todd Hopkinson
  • 6,803
  • 5
  • 32
  • 34