How to send mail from a custom UITableViewCell using MFMailComposerViewController [duplicate] - ios

This question already has answers here:
Objective C: Send email without leaving app
(3 answers)
Closed 3 years ago.
I want to send mail while clicking a button in a custom UITableViewCell using MFMailComposeViewController.
Appreciate if answer is in Objective-C.

Following code in your .h file
#import <MessageUI/MFMailComposeViewController.h>
Give Delegate <MFMailComposeViewControllerDelegate>
Following code in your .m file
//Where you want to open dialog write below code
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
mailCont.mailComposeDelegate = self; // Required to invoke mailComposeController when send
[mailCont setSubject:#"Your Subject!"];
[mailCont setToRecipients:[NSArray arrayWithObject:#"hello#test.com"]];
[mailCont setMessageBody:#"Your Body" isHTML:NO];
[self presentViewController:mailCont animated:YES completion:nil];
}
//Delegate Method
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
//YOUR ACTION
break;
case MFMailComposeResultSent:
//YOUR ACTION
break;
case MFMailComposeResultSaved:
//YOUR ACTION
break;
case MFMailComposeResultFailed:
//YOUR ACTION
break;
default:
break;
}
}
You can dismiss view by this code - [self dismissViewControllerAnimated:YES completion:nil];

Make sure you are testing it in Real device not in simulator and you have a mail ID configured in your device
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface ViewController ()<MFMailComposeViewControllerDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)sendmail:(id)sender {
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Subject"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"Recipients", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = #"Body";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentViewController:mailer animated:YES completion:nil];
}
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// Close the Mail Interface
[self dismissViewControllerAnimated:NO completion:NULL];
}

Related

how to compose mail when button at index clicked in UIActionSheet?

I am a bit confused on how to display mail composer when I click the email option in UIActionSheet.
Here's my sample code:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 0)
{
NSString *emailTitle = #"Test Email";
NSString *messageBody = #"iOS programming is so fun!";
NSArray *toRecipents = [NSArray arrayWithObject:#"support#email.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[self presentViewController:mc animated:YES completion:NULL];
}
}
Make sure you add to your .h file and import the MessageUI.framework
Here is exactly what your looking for. I use this often. By the way UIActionSheets are deprecated in iOS 8. Here you go though:
.h
ViewController : UIViewController <MFMailComposeViewControllerDelegate>
.m
- (IBAction)showActionSheet:(id)sender {
UIActionSheet *moreActions = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Add To Favorites", #"Search",#"Email", nil];
[moreActions showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: [self addToFav:self];
break;
case 1: ; [self popSearchBar:self];
break;
case 2: { [self sendEmail];
break;
}
break;
}
}
- (void)sendEmail {
NSString *emailTitle = #"This is email title";
// Email Content as for HTML
NSString *messageBody = [NSString stringWithFormat:#"I may have found a missing document in your catalog. I tried opening :<p><strong><font color=\"red\"> %# </font><br>'%#'</strong></p> with no results. Can I have a dollar for reporting this?", self.title, self.titleString];
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"youremail#google.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:YES];
[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");
[self dismissViewControllerAnimated:YES completion:NULL];
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
[self dismissViewControllerAnimated:YES completion:NULL];
break;
case MFMailComposeResultSent:
NSLog(#"Mail sent");
[self dismissViewControllerAnimated:YES completion:NULL];
break;
case MFMailComposeResultFailed:
NSLog(#"Mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
Also note that some people are having issues with simulator emails. try it on a device before you give up.
Here's the code:
First add and import the message framework:
#import <MessageUI/MessageUI.h>
then mark your self as a delegate like this
#interface MYViewController () <MFMailComposeViewControllerDelegate>
then to pull up the composer:
- (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];
}
in actionsheet using actionSheet: clickedButtonAtIndex:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Get the name of the current pressed button
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Your button title"]) {
[self btnContact];
}
}
call this method
- (void)btnContact{
// Email Subject
NSString *emailTitle = #"";
// Email Content
NSString *messageBody = #"";
// To address
NSString *toRecp = #"";
NSArray *toRecipents = [NSArray arrayWithObject:toRecp];
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];
}
This is appcoda's code . Instead of index use the button's name to fire your mail option .

How to attach a PDF file to mail and send mail in iOS

Currently I am developing an expense management app and I want to attach a PDF file to the mail and send it to the specified email Address. So guide me from where should I start this and how to develop it.
Thanks.
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;
[vc setSubject:#"Monthly Expense Report"];
[vc addAttachmentData:pdfData mimeType:#"application/pdf" fileName:[NSString stringWithFormat:#"%#.pdf",[arrOfExpenseDate objectAtIndex:1]]];
if ([MFMailComposeViewController canSendMail])
{
[self presentViewController:vc animated:YES completion:nil];
}
I have created a example for you on how to send an pdf attachment using MFMailComposeViewController. Following is the code :
#interface ViewController () <MFMailComposeViewControllerDelegate>
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)compseEmail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;
[vc setSubject:#"Monthly Expense Report"];
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"ApacheInstallationSteps" ofType:#"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
[vc addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"ApacheInstallationSteps.pdf"];
[self presentViewController:vc animated:YES completion:nil];
}
else
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Alert!!!" message:#"Your device can't send emails." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(#"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Result: failed");
break;
default:
NSLog(#"Result: not sent");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
Make sure that you add MessageUI.framework. Following is the output.
To send a pdf or any other attahcment in mail using MFMailComposeViewController, you have to use:
[mc addAttachmentData:fileData mimeType:mimeType fileName:filename];
For more details, follow this tutorial.
You can also download the sample code given in this tutorial for better understanding.
Hope that helps.

EmailDialog not closing when send or even cancelling

i want to send an email, but when i click on send it really sends that email but the view is not closed.
MainViewController.m
NSString *text = [self fetchLogCoreData];
PlistCreatorViewController *plistCreator = [[PlistCreatorViewController alloc] init];
[plistCreator sendPlist2Email:self text:text];
I'm fetching all data in coreData and then put it into an NSString.
Then call sendPlist2Email with self as the view and text as the text i want to use in email.
PlistCreatorViewController.m
-(void)sendPlist2Email: (UIViewController*) viewController text:(NSString*)text{
NSString *emailTitle = #"PickDPack App: DATA";
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:text isHTML:NO];
// Determine the file name and extension
// Get the resource path and read the file using NSData
// Add attachment
[viewController presentViewController:mc animated:YES completion:nil];
}
- (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;
}
MainViewController *mainView = [[MainViewController alloc] init];
// Close the Mail Interface
NSLog(#"Email_out");
[mainView dismissViewControllerAnimated:YES completion:NULL];
}
Really simple, im sending the email but the window doesn't close when i press send/cancel.
I've tried to put selfinto [mainView dismissViewControllerAnimated:YES completion:NULL]; changing mainViewbut nothing change.
I've put this code inside MainViewController.mbut nothing change.
I hope anyone could help me :S
Thx
Have you tried as like below?
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[controller dismissViewControllerAnimated:YES completion:NULL];
}

MFMailComposeViewController doesn't close

I have an app where I use E-Mail to developer Button. When I press the button VEMailView-controller opens.
This is just wrapper for MFMailComposeViewController.
When I press "send" button, the controller have to be closed but I see just white window. No more. It must be closed to main ViewController. How to fix?
This is my code:
#import <MessageUI/MessageUI.h>
#import "VEMailView.h"
#interface VEMailView () <
MFMailComposeViewControllerDelegate,
UINavigationControllerDelegate
>
// UILabel for displaying the result of the sending the message.
#end
#implementation VEMailView{
BOOL alreadyOpened;
}
- (void)viewDidLoad
{
[super viewDidLoad];
alreadyOpened = NO;
}
- (void) viewDidAppear:(BOOL)animated {
[self showMailPicker];
}
- (void)showMailPicker
{
if ([MFMailComposeViewController canSendMail])
{
[self displayMailComposerSheet];
}
}
- (void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"iOS"];
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
[picker setToRecipients:toRecipients];
NSString *emailBody = #" ";
[picker setMessageBody:emailBody isHTML:NO];
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Result: Mail sending canceled");
break;
case MFMailComposeResultSaved:
NSLog(#"Result: Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Result: Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Result: Mail sending failed");
break;
default:
NSLog(#"Result: Mail not sent");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
That's because you are making an extra view controller. Create the MFMailComposeViewController in the view controller where the button is. The mail compose controller is a controller by itself. There is a white screen because that is the default view of YOUR VEMailView. Get rid of that and put these methods:
- (void)showMailPicker;
- (void)displayMailComposerSheet;
in the view controller with the button. Also make it the delegate.
I found my own solution.
To solve this problem change
[self dismissViewControllerAnimated:YES completion:nil];
into
[self dismissViewControllerAnimated:YES completion:^{[self dismissViewControllerAnimated:YES completion:nil];}];
Thanks Michal for idea.

How to send an E-Mail inside of an app in Xcode?

I am new to xcode, and am wondering how to send email in an app! My code is below, but I keep getting the error "No visible #interface for 'jakem' declares the selector 'presentViewControllerAnimated:'". Is my code completely wrong? Or did I just forget to declare the selector, and how do I declare the selector? I have researched all over the internet for at least an hour, and nothing is working. Someone please help me!
-(IBAction)sendEmail{
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"FrankMurphy.CEO#RomansXIII.com", nil]];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:composer animated:YES];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if(error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"error" message:[NSString stringWithFormat:#"error %#", [error description]] delegate:nil cancelButtonTitle:#"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissViewControllerAnimated:YES];
}
else {
[self dismissViewControllerAnimated:YES];
}
}
in .h header file....
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface SimpleEmailViewController : UIViewController <MFMailComposeViewControllerDelegate> // Add the delegate
- (IBAction)showEmail:(id)sender;
#end
in .m implementation file.....
- (IBAction)showEmail:(id)sender {
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"info#finetechnosoft.in"];
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];
}
Check if you are MFMailComposeViewControllerDelegate.
You do this like
#interface YouClassName : UIViewController <MFMailComposeViewControllerDelegate>
#end
I think you're using the wrong method. Try
[self presentViewController:(UIViewController *) animated:(BOOL) completion:(void)completion];
instead of:
[self presentViewController:composer animated:YES];
I work for Sendgrid. We have an Objective-c library that lets you quickly send email from inside your app, https://github.com/sendgrid/sendgrid-objc. You can use cocoapods to quickly install the library in your project.
Then sending the email from your (IBAction) would look like this:
-(IBAction)sendEmail{
sendgrid *msg = [sendgrid user:#"username" andPass:#"password"];
msg.to = #"FrankMurphy.CEO#RomansXIII.com";
msg.from = #"me#bar.com";
msg.text = #"hello world";
msg.html = #"<h1>hello world!</h1>";
[msg sendWithWeb];
}

Resources