0

I wish to be able to build a simple email client to send emails.

I checked out the IOS programming 101 tutorial on building an email client using MessageUI framework, but what i want to implement in my app is slightly different.

What i want to achieve is that, when i press a Button, a precomposed email(will be written by me, for example, like YOLO) will be sent directly to the recipient.

Is there anyway i can achieve this?

PS:I do not want the Mail ComposeViewController at all, just a simple button for sending a precomposed mail.

Thanks a lot

Regards

Lorenzo
  • 3,293
  • 4
  • 29
  • 56
progammingBeignner
  • 936
  • 1
  • 8
  • 19

4 Answers4

10

Import the “MessageUI.h” and implement the “MFMailComposeViewControllerDelegate” delegate in your view controller.

#import <MessageUI/MessageUI.h> 

interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate> // Add the delegate


- (void)sendEmail {
    // Email Subject
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"Test Subject!";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"support@test.com"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];

    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];

}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}
Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
3

AFAIK, you can pre-compose and email, add recipients, etc. but still the MFMailComposeViewController must definitely appear.

You can't just send an email by the click of one button, without the user knowing what was sent, or to whom the mail was sent. It's against the Apple HIG as well.

A possible alternative could be to make an api call to the server, and make the server implement the emailing part. Something like :

NSURLRequest requestWithURL:[NSURL urlWithString:@"http://yourserver.com/send_preset_mail?to=email@xyz.com"]];
Mehul Parmar
  • 3,599
  • 3
  • 26
  • 42
  • Well, if that is true, that is just kinda sad.. haha, i am just trying out fun apps. Thanks for the tips though. Appreciate it. Edit: Ok, I will try to see if your second method works for me not. Thanks a lot – progammingBeignner Oct 08 '15 at 05:43
2

You can use third party library to achieve that. MailCore-iOS is a great 3rd party library for developing mail apps.

https://github.com/MailCore/mailcore2/blob/master/build-mac/README.md

Here it is described in detailed how to use I would suggest to use binary version, not cocoa-pods.

Alex
  • 616
  • 4
  • 11
0

You can create api for that. If you don't want to create api then you can use smtp protocol for sending email without opening MFMailComposer.

Open Source Cocoa/Cocoa-Touch POP3/SMTP library?

Community
  • 1
  • 1
Jayesh Mardiya
  • 770
  • 7
  • 17