Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Other/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 32 additions & 23 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0-rc.2"),
.package(url: "https://github.com/twof/VaporMailgunService.git", from: "0.4.0"),
.package(url: "https://github.com/vapor-community/sendgrid-provider.git", from: "3.0.1"),
.package(url: "https://github.com/LiveUI/VaporTestTools.git", .branch("master"))
],
targets: [
.target(name: "MailCore", dependencies: [
"Vapor",
"Mailgun"
"Mailgun",
"SendGrid"
]
),
.target(name: "MailCoreTestTools", dependencies: [
Expand Down
79 changes: 78 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,78 @@
# MailCore
![Vapor 3 test tools](https://github.com/LiveUI/VaporTestTools/raw/master/Other/logo.png)

##

[![Slack](https://img.shields.io/badge/join-slack-745EAF.svg?style=flat)](http://bit.ly/2B0dEyt)
[![Jenkins](https://ci.liveui.io/job/LiveUI/job/MailCore/job/master/badge/icon)](https://ci.liveui.io/job/LiveUI/job/MailCore/)
[![Platforms](https://img.shields.io/badge/platforms-macOS%2010.13%20|%20Ubuntu%2016.04%20LTS-ff0000.svg?style=flat)](https://github.com/LiveUI/Boost)
[![Swift Package Manager](https://img.shields.io/badge/SPM-compatible-4BC51D.svg?style=flat)](https://swift.org/package-manager/)
[![Swift 4](https://img.shields.io/badge/swift-4.0-orange.svg?style=flat)](http://swift.org)
[![Vapor 3](https://img.shields.io/badge/vapor-3.0-blue.svg?style=flat)](https://vapor.codes)


Mailing wrapper for multiple mailing services like MailGun, SendGrig or SMTP

## Features

- [x] MailGun
- [ ] SendGrid
- [ ] SMTP

## Install

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

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

## Use

Usage is really simple. First register the service in your apps `configure` method:

```swift
let config = Mailer.Config.mailgun(key: "{mailGunApi}", domain: "{mailGunDomain}")
Mailer(config: config, registerOn: &services)
```

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

And send an email:

```swift
let mail = Mailer.Message(from: "admin@liveui.io", to: "bobby.ewing@southfork.com", 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
}
```

## Support

Join our [Slack](http://bit.ly/2B0dEyt), channel <b>#help-boost</b> to ... well, get help :)

## Boost AppStore

Core package for <b>[Boost](http://www.boostappstore.com)</b>, a completely open source enterprise AppStore written in Swift!
- Website: http://www.boostappstore.com
- Github: https://github.com/LiveUI/Boost

## Other core packages

* [BoostCore](https://github.com/LiveUI/BoostCore/) - AppStore core module
* [ApiCore](https://github.com/LiveUI/ApiCore/) - Base user & team management including forgotten passwords, etc ...
* [DBCore](https://github.com/LiveUI/DbCore/) - Set of tools for work with PostgreSQL database
* [VaporTestTools](https://github.com/LiveUI/VaporTestTools) - Test tools and helpers for Vapor 3

## 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](https://github.com/rafiki270), [Twitter](https://twitter.com/rafiki270), [LiveUI Slack](http://bit.ly/2B0dEyt) and [Vapor Slack](https://vapor.team/))

## License

MIT license, please see LICENSE file for more details.

34 changes: 34 additions & 0 deletions Sources/MailCore/Extensions/Message+SendGrid.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Message+SendGrid.swift
// MailCore
//
// Created by Ondrej Rafaj on 11/04/2018.
//

import Foundation
import Vapor
import SendGrid


extension Mailer.Message {

func asSendGridContent() -> SendGridEmail {
var content = [
[
"type": "text/plain",
"value": text
]
]
if let html = html {
content.append(
[
"type": "text/html",
"value": html
]
)
}
let message = SendGridEmail(from: EmailAddress(email: from), replyTo: EmailAddress(email: to), subject: subject, content: content)
return message
}

}
22 changes: 20 additions & 2 deletions Sources/MailCore/MailCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import Vapor
import Mailgun
import SendGrid


public protocol MailerService: Service {
Expand Down Expand Up @@ -42,19 +43,24 @@ public class Mailer: MailerService {
public enum Config {
case none
case mailgun(key: String, domain: String)
case sendGrid(key: String)
}

let config: Config


// MARK: Initialization

@discardableResult public init(config: Config, registerOn services: inout Services) {
@discardableResult public init(config: Config, registerOn services: inout Services) throws {
self.config = config

switch config {
case .mailgun(let key, let domain):
services.register(Mailgun(apiKey: key, domain: domain), as: Mailgun.self)
case .sendGrid(key: let key):
let config = SendGridConfig(apiKey: key)
services.register(config)
try services.register(SendGridProvider())
default:
break
}
Expand All @@ -70,7 +76,19 @@ public class Mailer: MailerService {
let mailgunClient = try req.make(Mailgun.self)
return try mailgunClient.send(message.asMailgunContent(), on: req).map(to: Mailer.Result.self) { _ in
return Mailer.Result.success
}
}.catchMap({ error in
return Mailer.Result.failure(error: error)
}
)
case .sendGrid(_):
let email = message.asSendGridContent()
let sendGridClient = try req.make(SendGridClient.self)
return try sendGridClient.send([email], on: req.eventLoop).map(to: Mailer.Result.self) { _ in
return Mailer.Result.success
}.catchMap({ error in
return Mailer.Result.failure(error: error)
}
)
default:
return req.eventLoop.newSucceededFuture(result: Mailer.Result.serviceNotConfigured)
}
Expand Down
1 change: 0 additions & 1 deletion Tests/MailCoreTests/MailCoreTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import App
import Dispatch
import XCTest

Expand Down
5 changes: 2 additions & 3 deletions scripts/update.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env bash

rm -rf .build
vapor clean --verbose -y
rm Package.resolved
vapor xcode --verbose -y
vapor clean -y --verbose
vapor xcode -n --verbose
6 changes: 6 additions & 0 deletions scripts/upgrade.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

rm -rf .build
vapor clean -y --verbose
rm Package.resolved
vapor xcode -n --verbose