Skip to content

Instantly share code, notes, and snippets.

@AlexHedley
Created May 4, 2015 17:32
Show Gist options
  • Select an option

  • Save AlexHedley/6e306d13653113d8fcc6 to your computer and use it in GitHub Desktop.

Select an option

Save AlexHedley/6e306d13653113d8fcc6 to your computer and use it in GitHub Desktop.
Send an Email
#import <MessageUI/MessageUI.h>
@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>
- (void)sendEmail {
// Email Subject
NSString *emailTitle = @"Subject";
// Email Content
NSString *messageBody = @"\n\nSent from the iOS App";
// To address
//NSArray *toRecipents = [NSArray arrayWithObject:kEmail];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
//[mc setToRecipients:toRecipents];
//NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"X" ofType:@"pdf"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"X.pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:path];
[mc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"X"];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}];
}
- (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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment