问题
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-KEY
is 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