📧 Service to assist with sending emails from Vapor apps
⚠️Breaking changes⚠️
This release of mailgun allows a user to mock MailgunProvider
for testing and also uses the correct EventLoop
for Request
extension Application.Mailgun.Provider {
static var fake: Self {
.init {
$0.mailgun.use { app, _ in
MockMailgun(eventLoop: app.eventLoopGroup.next())
}
}
}
}
This release also renames the package name and url to be more concise. So now installing mailgun will look like
.package(url: "https://github.com/vapor-community/mailgun.git", from: "5.0.0")
.target(name: "App", dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "Mailgun", package: "mailgun")
])
Mailgun now supports Vapor 4! Development for Vapor 4 will be done on master
from here on out and Vapor 3 development will be done on the vapor3
branch.
import Mailgun
// Called before your application initializes.
func configure(_ app: Application) throws {
/// case 1
/// put into your environment variables the following keys:
/// MAILGUN_API_KEY=...
app.mailgun.configuration = .environment
/// case 2
/// manually
app.mailgun.configuration = .init(apiKey: "<api key>")
}
// call it without arguments to use default domain
app.mailgun().send(...)
req.mailgun().send(...)
// or call it with domain
app.mailgun(.myApp1).send(...)
req.mailgun(.myApp1).send(...)
You can now use multiple domains to send emails with Mailgun
let mailgun = Mailgun(apiKey: "<api key>")
services.register(mailgun, as: Mailgun.self)
// Put this extension at the bottom or create a new file for it
extension Mailgun.DomainConfig {
static var euDomain: Mailgun.DomainConfig {
return Mailgun.DomainConfig("mg.example.com", region: .eu)
}
static var usDomain: Mailgun.DomainConfig {
return Mailgun.DomainConfig("mg2.example.com", region: .us)
}
}
mailgun.send(message, domain: .euDomain, on: req)
The new major version for this release is 3.0.0 to align with the supported Vapor version. 4.0.0 tag coming soon.
In this release: #34
Thanks to @saicu for the implementation! https://documentation.mailgun.com/en/latest/user_manual.html#templates
let message = Mailgun.TemplateMessage(
from: "[email protected]",
to: "[email protected]",
subject: "Newsletter",
template: "my-template",
templateData: ["foo": "bar"]
)
let mailgun = try req.make(Mailgun.self)
return try mailgun.send(message, on: req)
let template = Mailgun.Template(name: "my-template", description: "api created :)", template: "<h1>Hello {{ name }}</h1>")
let mailgun = try req.make(Mailgun.self)
return try mailgun.createTemplate(template, on: req)
You can now add inline images that will display when the email is rendered. It uses the same format as image attachments.