IOS Swift Mailgun not sending email

天大地大妈咪最大 提交于 2020-06-28 04:04:48

问题


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: "me@mail.com", from: "Test User <myemail@mail.com", 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


回答1:


@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": "sender@whatyouwant.com",
                     "to": "anyRecipient@example.com",
                "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.



来源:https://stackoverflow.com/questions/45170799/ios-swift-mailgun-not-sending-email

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!