Swiftpack.co - Package - vapor-community/VaporMailgunService

Vapor Mailgun Service

Slack Platforms Swift 4.1 Vapor 3

Mailgun is a Vapor 3 service for a popular email sending API

Installation

Vapor Mailgun Service can be installed with Swift Package Manager

.package(url: "https://github.com/twof/VaporMailgunService.git", from: "1.5.0")

Usage

Sign up and set up a Mailgun account here

Make sure you get an API key and register a custom domain

Configure

In configure.swift:

let mailgun = Mailgun(apiKey: "<api key>", domain: "mg.example.com", region: .eu)
services.register(mailgun, as: Mailgun.self)

Note: If your private api key begins with key-, be sure to include it

Use

In routes.swift:

Without attachments

router.post("mail") { (req) -> Future<Response> in
    let message = Mailgun.Message(
        from: "postmaster@example.com",
        to: "example@gmail.com",
        subject: "Newsletter",
        text: "This is a newsletter",
        html: "<h1>This is a newsletter</h1>"
    )

    let mailgun = try req.make(Mailgun.self)
    return try mailgun.send(message, on: req)
}

With attachments

router.post("mail") { (req) -> Future<Response> in
    let fm = FileManager.default
    guard let attachmentData = fm.contents(atPath: "/tmp/test.pdf") else {
        throw Abort(.internalServerError)
    }
    let attachment = File(data: attachmentData, filename: "test.pdf")
    let message = Mailgun.Message(
        from: "postmaster@example.com",
        to: "example@gmail.com",
        subject: "Newsletter",
        text: "This is a newsletter",
        html: "<h1>This is a newsletter</h1>",
        attachments: [attachment]
    )

    let mailgun = try req.make(Mailgun.self)
    return try mailgun.send(message, on: req)
}

With template (attachments can be used in same way)

router.post("mail") { (req) -> Future<Response> in
    let message = Mailgun.TemplateMessage(
        from: "postmaster@example.com",
        to: "example@gmail.com",
        subject: "Newsletter",
        template: "my-template",
        templateData: ["foo": "bar"]
    )

    let mailgun = try req.make(Mailgun.self)
    return try mailgun.send(message, on: req)
}

Setup content through Leaf

Using Vapor Leaf, you can easily setup your HTML Content.

First setup a leaf file in Resources/Views/Emails/my-email.leaf

<html>
    <body>
        <p>Hi #(name)</p>
    </body>
</html>

With this, you can change the #(name) with a variable from your Swift code, when sending the mail

router.post("mail") { (req) -> Future<Response> in
    let content = try req.view().render("Emails/my-email", [
        "name": "Bob"
    ])

    let message = Mailgun.Message(
        from: "postmaster@example.com",
        to: "example@gmail.com",
        subject: "Newsletter",
        text: "",
        html: content
    )

    let mailgun = try req.make(Mailgun.self)
    return try mailgun.send(message, on: req)
}

Setup routes

public func boot(_ app: Application) throws {
    // sets up a catch_all forward for the route listed
    let routeSetup = RouteSetup(forwardURL: "http://example.com/mailgun/all", description: "A route for all emails")
    let mailgunClient = try app.make(Mailgun.self)
    try mailgunClient.setup(forwarding: routeSetup, with: app).map { (resp) in
        print(resp)
    }
}

Handle routes

mailgunGroup.post("all") { (req) -> Future<String> in
    do {
        return try req.content.decode(IncomingMailgun.self).map { (incomingMail) in
            return "Hello"
        }
    } catch {
        throw Abort(HTTPStatus.internalServerError, reason: "Could not decode incoming message")
    }
}

Creating templates

router.post("template") { (req) -> Future<Response> in
    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)
}

Github

link
Stars: 51
Help us keep the lights on

Dependencies

Releases

1.8.1 - Aug 27, 2019

In this release: #34

1.8.0 - Aug 3, 2019

Thanks to @saicu for the implementation! https://documentation.mailgun.com/en/latest/user_manual.html#templates

Sending templated emails

let message = Mailgun.TemplateMessage(
    from: "postmaster@example.com",
    to: "example@gmail.com",
    subject: "Newsletter",
    template: "my-template",
    templateData: ["foo": "bar"]
)

let mailgun = try req.make(Mailgun.self)
return try mailgun.send(message, on: req)

Setting up email templates

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)

1.7.0 - Jun 19, 2019

1.6.0 - May 10, 2019

You can now add inline images that will display when the email is rendered. It uses the same format as image attachments.

1.5.0 - Sep 18, 2018