GithubHelp home page GithubHelp logo

martinw / vapormailgunservice Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vapor-community/mailgun

0.0 2.0 0.0 50 KB

๐Ÿ“ง Service to assist with sending emails from Vapor apps

Swift 100.00%

vapormailgunservice's Introduction

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")
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: "[email protected]",
        to: "[email protected]",
        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: "[email protected]",
        to: "[email protected]",
        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)
}

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: "[email protected]",
        to: "[email protected]",
        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")
    }
}

vapormailgunservice's People

Contributors

foodevbar avatar martinw avatar mihaelisaev avatar rafiki270 avatar steffendsommer avatar twof avatar vkill avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.