Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone app send email

Tags:

email

iphone

I know how to send an email within my app by launching the Mail app then returning to my app... but I would like to my app to be able to send email without opening the mail app. For exemple i'd have a button in my app clicking that button would send out an email. I will then notify the user that the email has been sent...

Has anyone been doing this ?

thanks.

Sami

like image 375
sami Avatar asked Sep 29 '09 20:09

sami


2 Answers

Here is a sample code to send email using MFMailComposeViewController.

-(IBAction)showPicker:(id)sender {
// This sample can run on devices running iPhone OS 2.0 or later  
// The MFMailComposeViewController class is only available in iPhone OS 3.0 or later. 
// So, we must verify the existence of the above class and provide a workaround for devices running 
// earlier versions of the iPhone OS. 
// We display an email composition interface if MFMailComposeViewController exists and the device can send emails.
// We launch the Mail application on the device, otherwise.

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 {
// Displays an email composition interface inside the application. Populates all the Mail fields. 

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello from California!"];


// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"]; 
NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil]; 
NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"]; 

[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];  
[picker setBccRecipients:bccRecipients];

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

// Fill out the email body text
NSString *emailBody = @"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
[picker release];
}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {  
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of           the operation.
message.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
    case MFMailComposeResultCancelled:
        message.text = @"Result: canceled";
        break;
    case MFMailComposeResultSaved:
        message.text = @"Result: saved";
        break;
    case MFMailComposeResultSent:
        message.text = @"Result: sent";
        break;
    case MFMailComposeResultFailed:
        message.text = @"Result: failed";
        break;
    default:
        message.text = @"Result: not sent";
        break;
}
[self dismissModalViewControllerAnimated:YES];
 }

-(void)launchMailAppOnDevice {

// Launches the Mail application on the device.
NSString *recipients = @"mailto:[email protected][email protected],[email protected]&subject=Hello from California!";
NSString *body = @"&body=It is raining in sunny California!";

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

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
like image 72
Chintan Patel Avatar answered Sep 23 '22 18:09

Chintan Patel


You have a few choices. You can use Apple's MFMailComposeViewController class (see below) which allows you to make a message in your app and pass it to iPhone's Mail, without launching the Mail app or leaving yours. You can also implement SMTP in your app to send e-mail directly. You can also hand off your email to a webserver and have the webserver send it out. The easiest is the first way. The drawback is that you don't really know if the message was sent out or not, which depends on whether the network was operational or not and other factors. Of course, if you go with your own SMTP code, you will have to handle all the queuing and retrying in case the network, or server is unavailable, and that means your app has to be running in order to do that.

From Apple's docs:

The MFMailComposeViewController class provides a standard interface that manages the editing and sending an email message. You can use this view controller to display a standard email view inside your application and populate the fields of that view with initial values, such as the subject, email recipients, body text, and attachments. The user can edit the initial contents you specify and choose to send the email or cancel the operation.

like image 36
mahboudz Avatar answered Sep 23 '22 18:09

mahboudz