SWMailgun provides a simple alternative when you need to send an email with your iOS app using MailGun.
Sometimes, there is the need to setup a simple email form in your iOS app, or to trigger an email after an action without having to setup your own service for that, sometimes you don't want to use the MailComposeViewController
or use a SMTP
library.
This provide a simple alternative when you need to send an email with your iOS app.
Mailgun provides a simple reliable API for transactional emails. You will need to have an ApiKey
and an account to use the client.
- iOS 9.3+
- Swift 5.0+
SWMailgun is available through SPM. To install it, follow the steps:
Open Xcode project > File > Swift Packages > Add Package Dependecy
After that, put the url in the field: https://github.com/thejohnlima/SWMailgun.git
SWMailgun is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SWMailgun'
and run pod install
Usage is very simple
// MARK: - Example using html string
import SWMailgun
let service = MailgunService()
let html = "<b>Test</b>"
let email = MailgunEmail(
from: "Excited User <[email protected]>",
to: "[email protected]",
subject: "This is a test",
html: html
)
let auth = MailgunAuth(
domain: "YOUR_DOMAIN",
apiKey: "YOUR_API_KEY"
)
service.send(email: email, auth: auth) { result, error in
guard let result = result else {
print("Error: \(error)")
return
}
print("Email was sent: \(result.isSent())")
}
// MARK: - Example using html template
// You can create your html template in Mailgun web site
import SWMailgun
let service = MailgunService()
let parameters = ["user_name": "John", "temporary_password": "johnjohn123"]
guard let variables = try? JSONSerialization.data(withJSONObject: [parameters], options: .prettyPrinted) else {
print("❌ Something wrong. Check your parameters")
return
}
let email = MailgunEmail(
from: "Excited User <[email protected]>",
to: "[email protected]",
subject: "This is a test",
template: "forgot_password",
variables: variables
)
let auth = MailgunAuth(
domain: "YOUR_DOMAIN",
apiKey: "YOUR_API_KEY"
)
service.send(email: email, auth: auth) { result, error in
guard let result = result else {
print("Error: \(error)")
return
}
print("Email was sent: \(result.isSent())")
}
If you need more examples, open demo project
.
- If you found a bug, open an issue.
- If you have a feature request, open an issue.
- If you want to contribute, submit a pull request. 👨🏻💻
SWMailgun is under MIT license. See the LICENSE file for more info.