GithubHelp home page GithubHelp logo

mailcore's Introduction

LiveUI MailCore

Slack Jenkins Platforms Swift Package Manager Swift 4 Vapor 3

Mailing wrapper for multiple mailing services like Mailgun, SendGrid or SMTP

Features

  • Mailgun
  • SendGrid
  • SMTP
  • Attachments
  • Multiple emails sent at the same time
  • Multiple recipint, CC & BCC fields

Install

Just add following line package to your Package.swift file.

.package(url: "https://github.com/LiveUI/MailCore.git", .branch("master"))

Usage

Usage is really simple mkey!

1/3) Configure

First create your client configuration:

Mailgun

let config = Mailer.Config.mailgun(key: "{mailgunApi}", domain: "{mailgunDomain}", region: "{mailgunRegion}")

SendGrid

let config = Mailer.Config.sendGrid(key: "{sendGridApiKey}")

SMTP

Use the SMTP struct as a handle to your SMTP server:

let smtp = SMTP(hostname: "smtp.gmail.com",     // SMTP server address
                   email: "[email protected]",     // username to login
                password: "password")           // password to login
                
let config = Mailer.Config.smtp(smtp)

SMTP using TLS

All parameters of SMTP struct:

let smtp = SMTP(hostname: String,
                   email: String,
                password: String,
                    port: Int32 = 465,
                  useTLS: Bool = true,
        tlsConfiguration: TLSConfiguration? = nil,
             authMethods: [AuthMethod] = [],
             accessToken: String? = nil,
              domainName: String = "localhost",
                 timeout: UInt = 10)
                 
let config = Mailer.Config.smtp(smtp)

2/3) Register service

Register and configure the service in your apps configure method.

Mailer(config: config, registerOn: &services)

Mailer.Config is an enum and you can choose from any integrated services to be used

3/3) Send an email

let mail = Mailer.Message(from: "[email protected]", to: "[email protected]", subject: "Oil spill", text: "Oooops I did it again", html: "<p>Oooops I did it again</p>")
return try req.mail.send(mail).flatMap(to: Response.self) { mailResult in
    print(mailResult)
    // ... Return your response for example
}

Testing

Mailcore provides a MailCoreTestTools framework which you can import into your tests to get MailerMock.

To register, and potentially override any existing "real" Mailer service, just initialize MailerMock with your services.

// Register
MailerMock(services: &services)

// Retrieve in your tests
let mailer = try! req.make(MailerService.self) as! MailerMock

MailerMock will store the last used result as well as the received message and request. Structure of the moct can be seen below:

public class MailerMock: MailerService {

    public var result: Mailer.Result = .success
    public var receivedMessage: Mailer.Message?
    public var receivedRequest: Request?

    // MARK: Initialization

    @discardableResult public init(services: inout Services) {
        services.remove(type: Mailer.self)
        services.register(self, as: MailerService.self)
    }

    // MARK: Public interface

    public func send(_ message: Mailer.Message, on req: Request) throws -> Future<Mailer.Result> {
        receivedMessage = message
        receivedRequest = req
        return req.eventLoop.newSucceededFuture(result: result)
    }

    public func clear() {
        result = .success
        receivedMessage = nil
        receivedRequest = nil
    }

}

Support

Join our Slack, channel #help-boost to ... well, get help :)

Enterprise AppStore

Core package for Einstore, a completely open source enterprise AppStore written in Swift!

Other core packages

  • EinstoreCore - AppStore core module
  • ApiCore - Base user & team management including forgotten passwords, etc ...

Implemented thirdparty providers

Code contributions

We love PR’s, we can’t get enough of them ... so if you have an interesting improvement, bug-fix or a new feature please don’t hesitate to get in touch. If you are not sure about something before you start the development you can always contact our dev and product team through our Slack.

Author

Ondrej Rafaj (@rafiki270 on Github, Twitter, LiveUI Slack and Vapor Slack)

License

MIT license, please see LICENSE file for more details.

mailcore's People

Contributors

airzona avatar maxdesiatov avatar rafiki270 avatar vzsg 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.