I want to send an email from my iPhone application. I have heard that the iOS SDK doesn't have an email API. I don't want to use the following code because it will exit my application:
NSString *url = [NSString stringWithString: @"mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
So how can I send an email from my app?
Heres a Swift version:
Source
If you want to send email from your application, the above code is the only way to do it unless you code your own mail client (SMTP) inside your app, or have a server send the mail for you.
For example, you could code your app to invoke a URL on your server which would send the mail for you. Then you simply call the URL from your code.
Note that with the above code you can't attach anything to the email, which the SMTP client method would allow you to do, as well as the server-side method.
I wrote a simple wrapper called KRNSendEmail that simplify sending email to one method call.
The KRNSendEmail is well documented and added to CocoaPods.
https://github.com/ulian-onua/KRNSendEmail
On iOS 3.0 and later you should use the
MFMailComposeViewController
class, and theMFMailComposeViewControllerDelegate
protocol, that is tucked away in the MessageUI framework.First add the framework and import:
Then, to send a message:
Then the user does the work and you get the delegate callback in time:
Remember to check if the device is configured for sending email:
MFMailComposeViewController is the way to go after the release of iPhone OS 3.0 software. You can look at the sample code or the tutorial I wrote.