Sending email without presenting a view - ios

I need send a email without presenting a MFMailComposeViewController In a few words, I need to send the E-mail directly without the user pressing the send button and to show the view.
My code is:
MFMailComposeViewController *mail = [[MFMailComposeViewController >alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Recordatorio de clave"];
[mail setMessageBody:message isHTML:NO];
[mail setToRecipients:#[email]];
[self presentModalViewController:mail animated:YES];

My answer is YES, You can send email from background without presenting MFMailComposeVC
There are two ways to do this
1- Either write a webservice that will send an email from background when it will be called.
2- You can send email by using SMTP gmail account. For this you will have to use SKPSMTPMessage Library. Here is a good tutorial for it.

if you don't want to use MFMailComposeViewController, you can use SMTP for this. please check this link Sending e-mail in background from iOS apps using SMTP gmail account

This is not possible with MFMailComposeViewController
You can create web service and pass data to it and your server can send mail.

You can't directly use MFMailComposeViewController for that need to external library or program need to us for sending mail using SMTP here I mention gmail thru sending mail without interaction of users.
Code for .h file
#import <UIKit/UIKit.h>
#import "SKPSMTPMessage.h"
#interface mailTransferViewController : UIViewController<SKPSMTPMessageDelegate>{
IBOutlet UITextField *emailField;
}
- (IBAction)sendMessageInBack:(id)anObject;
#end
Code for .m file
- (IBAction)sendMessageInBack:(id)anObject{
NSLog(#"Start Sending");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:#"sample.pdf"];
NSData *dataObj = [NSData dataWithContentsOfFile:writableDBPath];
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
testMsg.fromEmail = #"Yours mail ids";
testMsg.toEmail = emailField.text;//sender mail id
testMsg.relayHost = #"smtp.gmail.com";
testMsg.requiresAuth = YES;
testMsg.login = #"Your mail ids";
testMsg.pass = #"Mail id password";
testMsg.subject = #"Test application ";
testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS!
// Only do this for self-signed certs!
// testMsg.validateSSLChain = NO;
testMsg.delegate = self;
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/plain",kSKPSMTPPartContentTypeKey,#"Some text to include in body",kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
//Logic for attach file.
// NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"sample.pdf\"",kSKPSMTPPartContentTypeKey,#"attachment;\r\n\tfilename=\"sample.pdf\"",kSKPSMTPPartContentDispositionKey,[dataObj encodeBase64ForData],kSKPSMTPPartMessageKey,#"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
// NSLog(#"%#",vcfPart);
// testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];
// testMsg.parts = [NSArray arrayWithObjects:plainPart,vcfPart,nil];
testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
[testMsg send];
}

No, its not possible to send the mail. In the iOS you can not perform like that, you have to write a web based API for doing that.

That's not possible the user only has the permit to send the mail from the device. You can only compose the email. If you have to archive such kind of functionality just create it on Backend and trigger it from the device by calling webservice.

Sending emails programmatically, without user intervention, from an iphone application, cannot be implemented using any of the Apple frameworks. It could be possible in a jailbroken phone but then it would never see the inside of App Store.
If you want control of email sending, then a better way would be to set up a web service (at your server end) you can post to using an HTTP request. If you are posting to only one address this can work very well, although you may want to get the user to input their return mail address.
Otherwise only the standard dialog is available (this relies on using whatever account they've setup on the device).

Related

How to invite for an iCloud calendar event using mail, iOS swift

I have events (fetched using iCloud account) in my calendar, now I want to invite and share an event via contacts or mail. On invitation I need to attach this event details. The details will also be appeared on those calendars who are invited by me, when they click on invitation. Can anyone suggest the method to follow.
There are no methods of EKEvent which allows you to save it to file. The only way I see is to create file on your own, it should have VCalendar (.iCal extension) format. Just save event to file according to suggested below library. You can use mail composer or UIActivityViewController to share event to invite people.
Library URL : https://github.com/mysterioustrousers/EKEventToiCal
To attach it along with the email in the MFMailComposer using this below code:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *WritableDBPath= [documentsDirectory stringByAppendingPathComponent:kFilename];
NSData *data = [NSData dataWithContentsOfFile:WritableDBPath];
[picker addAttachmentData:data mimeType:#"text/calendar" fileName:#"/abc.ical"]; [picker setSubject:#"Database"];
[picker setMessageBody:#"Database testing" isHTML:NO];
[self presentModalViewController:picker animated:YES];
Let me know if you have any queries.

iOS - sending URL through mail showing different behavior in simulator and device

A strange behaviour is happening when I am trying to send mail from device.
I have used SMTP to send mail from background in my app and I have to send user's current location URL in app.
Now, when I send it from simulator it works almost fine and i got this url -
https://maps.google.com/maps?f=q&q=0.000000,0.000000
But, when I send it from device it sends the url like this -
https://maps.google.com/maps?f=q&q".719793,75.877068
The code I used to make url is
-(NSString*)LocationLinkTosentInMail
{
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
NSLog(#"*dLatitude : %#", latitude);
NSLog(#"*dLongitude : %#",longitude);
NSString *currentLocationURL = [NSString stringWithFormat:#"https://maps.google.com/maps?f=q&q=%#,%#",latitude,longitude];
return currentLocationURL;
}
NSURL *locationURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#",[self LocationLinkTosentInMail]]];
In SMTP mail function, I use this code to make dictionary
NSDictionary *plain_text_part = [NSDictionary dictionaryWithObjectsAndKeys:
#"text/plain\r\n\tcharset=UTF-8;\r\n\tformat=flowed", kSKPSMTPPartContentTypeKey,
[NSString stringWithFormat:#"My Location is: %#",locationURL], kSKPSMTPPartMessageKey,
#"quoted-printable", kSKPSMTPPartContentTransferEncodingKey,
nil];
Can anyone suggest me if any change required in code?
Why simulator and device are showing different behavior to send this url?
Use HTML Tag and in mail body enable HTML in iOS
[emailDialog setMessageBody:mailBody isHTML:YES];
Check this page for href tag!
Whenever we have to send URL thourgh smtp in objective c, we should always careful about the special character of url.
Finally i got the solution of my problem in the encoding process.
the Encoding in the NSDictionary object, I changed it from quoted-printable
to 8bit .
Now dictionary obj would be:
NSDictionary *plain_text_part = [NSDictionary dictionaryWithObjectsAndKeys:
#"text/plain", kSKPSMTPPartContentTypeKey,
strMessage, kSKPSMTPPartMessageKey,
#"8bit", kSKPSMTPPartContentTransferEncodingKey,
nil];

MFMailComposeViewController canSendMail returns YES but not sending mail in iOS

The following is my code to send an attachment in a mail. This works fine. I am able to send mails but I don't always receive the mails.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// Set the subject of email
[picker setSubject:#"My data file"];
// Add email addresses
[picker setToRecipients:[NSArray arrayWithObjects:emailId, nil]];
// Fill out the email body text
NSString *emailBody = #"Hello, \n Please find the data from the iOS app in the attachments.\n\n Thank you.\nMy Team.";
// This is not an HTML formatted email
[picker setMessageBody:emailBody isHTML:NO];
// Create NSData object from file
NSData *exportFileData = [NSData dataWithContentsOfFile:filePath];
// Attach image data to the email
[picker addAttachmentData:exportFileData mimeType:#"text/csv" fileName: [self.CSVNameTextField text]];
// Show email view
if ([MFMailComposeViewController canSendMail]) {
[self presentModalViewController:picker animated:YES];
}
After you send the Mail with your App go to the Mail-Software on your iPhone, you will most likely find the mail in the Outbox.
Cause the MFMailComposeViewController will just forward the Mail to the Mail-Software and it doesn't care what happens next to the message. So it's up to your Mail-Software how and when the Outbox will be updated.
I had the same problem, I found that it would go through the mail box and say it had sent and then nothing would come through.
About 20-30 minutes later the first one came through and then gradually the rest I sent came through too.
I don't know why it takes so long, if I find out I will edit this answer, but definitely wait up to an hour before assuming your code is broken.
Hope this helps someone who, like me, might be trawling their code over and over
In my case i had to manually open up the iphone mail app, then mails were sent and received immediately.

MFMailComposeViewController IOS

At the moment I have a NSArray of emails, and I open a view to end an email to all of these emails:
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"App Support"];
NSArray *toRecipients = [NSArray arrayWithArray:emails];
[mailer setToRecipients:toRecipients];
NSString *emailBody = #"";
[mailer setMessageBody:emailBody isHTML:NO];
// only for iPad
mailer.modalPresentationStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:mailer animated:YES];
Is there a way to send an email to this list without opening actually opening the controller (the user won't need to press on the send button and can't change the message)??
There's no way to send the message using the MFMailComposeViewController.
If you want to send an email "silently", I have previously used SKPSMTPMessage - an SMTP client that can be used on iOS to send emails without any UI.
You can set up a Gmail account specifically for the purpose of sending the messages if required.
I've created a simple demo for you. Download it here. Note that, along with the files in the SMTP folder, you will need to link to the CFNetwork.framework in your project.

iOS - Is it possible to send in continuos connection request to a server?

I have a app code that sends in request only when the app opens. However I need to have a continuos access to the file in the server which will be updated every second.
-(void)viewDidLoad
{
[super viewDidLoad];
NSURL *myurl = [NSURL URLWithString:#"URL"];
NSString *mystring = [NSString stringWithContentsOfURL:myurl encoding:NSASCIIStringEncoding error:nil];
//myTextView.text = realtime;
//NSLog(mystring);
NSString *stripped1 = [mystring stringByReplacingOccurrencesOfString:#"\r" withString:#""];
NSArray *rows = [stripped1 componentsSeparatedByString:#"\n"];
}
Is there a way that I can keep checking on my server file to load the data? Currently I can load it only once when I open the application. But its a live update application.
Thank you.
You may use apple notification to notify the user new data.
But I'm not sure if this is what you want.

Resources