nachshon f
nachshon f

Reputation: 3680

How to send an automatic email on an ios device using swift?

When a user created an account on my IOS app, I want an automation "thank you" email sent to him, how do i do that? I saw a company called mailgun.com, but they do not support swift. Does Parse support this? What do i do?

If parse supports this, please include a link for how to set this up. Here is the code i will use.

self.loginComplete("[email protected]")
func loginComplete(Email:String) {

\\send automatic email to the user.



}

Upvotes: 1

Views: 6958

Answers (2)

ammerzon
ammerzon

Reputation: 1078

Like Duncan C already mentioned you can't send email from your iOS app. The only possible solution is to use a server which provides an API to send emails.

I've made a sample code if you want to use mailgun.com:

let request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "https://api.mailgun.net/v3/{YOUR SANDBOX DOMAIN}/messages")!)
request.HTTPMethod = "POST"

// Basic Authentication
let username = "api"
let password = "{YOUR API-KEY}"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions([])
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")

let bodyStr = "from=Mailgun Sandbox <{YOUR SANDBOX DOMAIN}>&to=Receiver name <{YOUR RECEIVER EMAIL}>&subject=Test&text=thank you!"

// appending the data       
request.HTTPBody = bodyStr.dataUsingEncoding(NSUTF8StringEncoding);

let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
    // ... do other stuff here
})

task.resume()

Objective C

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.mailgun.net/v3/{YOUR SANDBOX DOMAIN}/messages"]];
request.HTTPMethod = @"POST";

NSString *username = @"api";
NSString *password = @"{YOUR API-KEY}";
NSString *loginString = [NSString stringWithFormat:@"%@:%@", username, password];
NSData *loginData = [loginString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64LoginString = [loginData base64EncodedStringWithOptions:0];
NSString *requestValue = [NSString stringWithFormat:@"Basic %@", base64LoginString];
[request setValue:requestValue forKey:@"Authorization"];

NSString *bodyStr = @"from=Mailgun Sandbox <{YOUR SANDBOX DOMAIN}>&to=Receiver name <{YOUR RECEIVER EMAIL}>&subject=Test&text=thank you!";

// appending the data
request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    // ... do other stuff here
}];

[task resume];

Note

Due to the new App Transport Security you have to add a declaration to the Info.plist file that specifies the Mailgun API because they don't support the latest SSL technology at the time of this writing. Here is a link and an example configuration:

<key>NSAppTransportSecurity</key>   
<dict>      
    <key>NSExceptionDomains</key>       
    <dict>          
        <key>mailgun.net</key>  
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSTemporaryExceptionMinimumTLSVersion</key>
            <string>TLSv1.1</string>        
        </dict>     
    </dict>     
</dict>

Upvotes: 11

Duncan C
Duncan C

Reputation: 131426

The short answer is, "You can't". Apple does not want apps to be able to send emails from the user's mail account unless the user initiates the send. You have to use Apple's MFMailComposeViewController, which can present the email to the user, ready to send, and then the user taps the send button to actually send.

If you want to send automatically you would need to connect to a server that supports sending email, and you would not be able to use the user's email account to do the sending. You'd have to use email credentials that you had embedded in the app (or prompt the user for his credentials)

Upvotes: 2

Related Questions