Stalker
Stalker

Reputation: 47

IOS Swift Mailgun not sending email

I am trying to send Email using MailGun api with Swift. I created and activate free account with mailgun. Installed pod.

cocoapods mailgun pod

If I press button I am getting message "Email was sent" but I am not receiving this email, nor it is displays in mailgun "Logs" or "Reporting".

I have also added and verified my personal e-mail to "Authorized Recipients"

I tied to run on IOS simulator and actual devices no luck.

   @IBAction func dddd(_ sender: Any) {

    let mailgun = MailgunAPI(apiKey: "key-<my_key from mailgun>, clientDomain: "sandboxe437***********.mailgun.org")

    mailgun.sendEmail(to: "[email protected]", from: "Test User <[email protected]", subject: "This is a test15", bodyHTML: "<b>test<b>") { mailgunResult in

        if mailgunResult.success{
            print("Email was sent")
        }else{
            print("error")
        }

}

Any word of advise what did I missed?

Thank you,

Stalker

Upvotes: 1

Views: 474

Answers (1)

Gabriel
Gabriel

Reputation: 213

@Stalker, your from parameter does not have a closing >. I hope you have seen it. If you are already using Alamofire for your network requests then no need for this extra dependency mailgun pod:

Swift 3.2

 import Alamofire
    
    let parameters = [
                   "from": "[email protected]",
                     "to": "[email protected]",
                "subject": "Subject of the email",
                   "text": "This is the body of the email."]
    let header = [
            "Authorization": "Basic YOUR-BASE64ENCODED-KEY",
            "Content-Type" : "application/x-www-form-urlencoded"]

    let url = "https://api.mailgun.net/v3/YOUR-DOMAIN/messages"
    Alamofire.request(url,
                   method: .post,
               parameters: parameters,
                 encoding: URLEncoding.default,
                  headers: header)
            .responseJSON { response in
                print("Response: \(response)")
                }

In the header, you have to replace YOUR-BASE64ENCODED-KEY with the base64 encoded string of "API:YOUR-SECRET-API-KEY" where YOUR-SECRET-API-KEYis found on your Mailgun dashboard.

In the URL you also replace YOUR-DOMAIN with your Mailgun domain.

With that you should be good to go and send emails through Mailgun.

Upvotes: 3

Related Questions