iOS - sending email - ios

I'm sending an email with very trivial method
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMessageComposeViewController.h>
-(void) sendEmailto: (NSArray*)p_recipient withSubject:(NSString*)p_subject body:(NSString*)p_body andAttachment:(NSData*)p_attachment
{
MFMailComposeViewController *emailComposer = [[MFMailComposeViewController alloc] init];
emailComposer.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail] == YES)
{
[emailComposer setSubject:p_subject];
if(p_recipient != nil)
{
[emailComposer setToRecipients:p_recipient];
}
if (p_body!= nil && [p_body isEqualToString:#""]==NO)
{
[emailComposer setMessageBody:p_body isHTML:NO];
}
if(p_attachment != nil)
{
[emailComposer addAttachmentData:p_attachment mimeType:#"image/jpeg" fileName:#"image.jpg"];
}
// Present mail view controller on screen
[emailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:emailComposer animated:YES];
self.sentEmailTargetController = p_target;
}
else
{
NSLog(#"Can't Open Email");
}
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"e-mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"e-mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"e-mail sent");
break;
case MFMailComposeResultFailed:
NSLog(#"e-mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
if (![[self presentedViewController] isBeingDismissed])
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
}
I have the declared MFMailComposeViewControllerDelegate delegate in the interface
running it on my device the email console opens, I can write a message, clicking on send closes the email window but doesn't actually send the email. The method - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error logs email sent but the mail does not reach it's destination. tried several email accounts.

I can write a message, clicking on send closes the email window but doesn't actually send the email
This is in fact within the spec, as documented:
Using this interface does not guarantee immediate delivery of the corresponding email message
Lots of things can go wrong, lack of Internet connection being among the most obvious. And of course even if the email is sent, that is no guarantee that it will reach anyone; other things can go wrong down the line.

Related

MFMessageComposeViewController not returning to application after send message ios9

Previously on iOS7, I have tested my SMS module and it worked nicely. After updating the iOS version, I noticed that the SMS module have some problems.
In my .h file
#import <MessageUI/MFMessageComposeViewController.h>
#interface ViewController : UIViewController<UITextFieldDelegate,MFMessageComposeViewControllerDelegate>
In my .m file
- (void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
if (result == MessageComposeResultCancelled){
NSLog(#"Message cancelled");
}
else if (result == MessageComposeResultSent){
NSLog(#"Message sent");
}
else{
NSLog(#"Message failed");
}
}
After I press send, in the log have show "Message sent" but the view is still at the message screen. I have no idea why it will not go back to my application.
Need help to find the problem to why it will not go back to my application.
Thanks in advance.
It seems you are not dismissing the mailcomposer after it is presented. You will have to dismiss the presented MFMessageComposeViewController in the following method:
-(void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
if (result == MessageComposeResultCancelled){
NSLog(#"Message cancelled");
}
else if (result == MessageComposeResultSent){
NSLog(#"Message sent");
}
else{
NSLog(#"Message failed");
}
[self dismissViewControllerAnimated:YES completion:nil]; //<---- This line
}
Moreover, - (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated is deprecated since iOS 6. Use - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion instead like this:
[self presentViewController:mailComposer animated:YES completion:nil];

dismissViewControllerAnimated: does not work

I have a method to send an email from my table view.
So one of the cells is a "contact us" cell, and whenever it's tapped I get to the mail view controller using MFMailComposeViewController , but the problem is that when I tap "cancel" or "send" my mail view controller does not go back to the table view. the "cancel" and "send" actions work, but I stay on the mail view controller.
This is the relevant methods:
- (void)sendEmail {
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"kkk#gmail.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];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SettingsControllerSection section = indexPath.section;
if (OtherControllerSection == section) {
SettingsControllerOtherSection row = indexPath.row;
switch (row) {
case RateUsController:
NSLog(#"rate us was pressed");
break;
case ContactUsControllerRow:
[self sendEmail];
NSLog(#"send email was pressed");
break;
}
}
}
please help, thanks!!
In mailComposeController method dismiss it like this:
[controller dismissViewControllerAnimated:YES completion:NULL];

Sending an email without user interaction

I am working on an app that automatically sends me an email message in certain circumstances but
I am unable to automatise the sending of the email, as the message composer picker comes up and I am required to physically press on the Send button...
Is it possible to automatise the "pressure" on the SEND button or does Apple prevent this to avoid spamming maybe?
What are the options for "completion"?
If this is not possible, is it then possible to send the email without using the message picker?
To bring up the message interface I'm using:
[self presentViewController:picker 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;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
It is not possible to send an email from the user's mail account without MFMailComposeViewController and user's explicit interaction.

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];
}

Passing Data from ViewController to iOS email app

Excuse me if im missing something I have only started this iOS dev yesterday. I have a basic application where a user can snap up to 3 pictures, these are held in 3 different UIImageViews, the app also retrieves the gps coordinates and changes these into an address.
What I want to do is be able to send these images and information to an email address.
Currently I have a send button which is linked to an action.
Thanks
- (IBAction)sendFinalItem:(UIButton *)sender {
NSLog(#"send button pressed");
MFMailComposeViewController *mailcontroller = [[MFMailComposeViewController alloc] init];
[mailcontroller setMailComposeDelegate:self];
NSString *email =#"MYEMAIL#MYEMAIL.CO.UK";
NSArray *emailArray = [[NSArray alloc] initWithObjects:email, nil];
[mailcontroller setToRecipients:emailArray];
[mailcontroller setSubject:#"[Urgent]Potential Job, iPhone snapped"];
[self presentViewController:mailcontroller 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;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
In your sendFinalItem method write this..
NSData *dataImage = UIImageJPEGRepresentation(yourImageViewName.image, 0.5f);
[mailer addAttachmentData:dataImage mimeType:#"image/jpeg" fileName:#"Image_1.jpg"]
Hope this helps.
EDIT:
[mailer setMessageBody:yourTextView.text isHTML:NO];
You can set isHTML to YES.(If your textview contains HTML data.)
EDIT:
If you want good quality image then use UIImagePNGRepresentation(yourImageViewName.image) instead of UIImageJPEGRepresentation(yourImageViewName.image, 0.5f)
Happy coding..

Resources