Swiftpack.co - Package - Mikroservices/Smtp

Smtp

Build Status Swift 5.2 Vapor 4 Swift Package Manager Platforms OS X | Linux

:email: SMTP protocol support for the Vapor web framework.

This framework has dependencies only to Vapor and SwiftNIO packages. SwiftNIO support was inspired by Apple examples: Swift NIO examples.

Features:

  • ☑ Vapor provider/service
  • ☑ SwiftNIO Support
  • ☑ Text/HTML
  • ☑ Attachments
  • ☑ SSL/TLS (when connection starts)
  • ☑ STARTTLS support
  • ☑ Multiple recipients & CC
  • ☑ Reply to
  • ☐ BCC fields
  • ☐ Multiple emails sent at the same time

Getting started

You need to add library to Package.swift file:

  • add package to dependencies:
.package(url: "https://github.com/Mikroservices/Smtp.git", from: "2.0.0")
  • and add product to your target:
.target(name: "App", dependencies: [
    .product(name: "Vapor", package: "vapor"),
    .product(name: "Smtp", package: "Smtp")
])

Set the SMTP server configuration (e.g. in main.swift file).

import Smtp

var env = try Environment.detect()
try LoggingSystem.bootstrap(from: &env)

let app = Application(env)
defer { app.shutdown() }

app.smtp.configuration.host = "smtp.server"
app.smtp.configuration.username = "johndoe"
app.smtp.configuration.password = "passw0rd"
app.smtp.configuration.secure = .ssl

try configure(app)
try app.run()

Using SMTP client.

let email = Email(from: EmailAddress(address: "john.doe@testxx.com", name: "John Doe"),
                  to: [EmailAddress(address: "ben.doe@testxx.com", name: "Ben Doe")],
                  subject: "The subject (text)",
                  body: "This is email body.")

request.smtp.send(email).map { result in
    switch result {
    case .success:
        print("Email has been sent")
    case .failure(let error):
        print("Email has not been sent: \(error)")
    }  
}

Also you can send emails directly via application class.

app.smtp.send(email).map { result in
    ...
}

Troubleshoots

You can use logHandler to handle and print all messages send/retrieved from email server.

request.smtp.send(email) { message in
    print(message)
}.map { result in
    ...
}

Developing

After cloning the repository you can open it in Xcode.

$ git clone https://github.com/Mikroservices/Smtp.git
$ cd Smtp
$ open Package.swift

You can build and run tests directly in Xcode.

Testing

Unit (integration) tests requires correct email credentials. Credentials are not check-in to the repository. If you want to run unit tests you have to use your mailtrap account and/or other email provider credentials.

All you need to do is replacing the configuration section in Tests/SmtpTests/SmtpTests.swift file.

Github

link
Stars: 9

Releases

Fix protection level of send method - 2020-06-01 07:51:00

Add possibility to use Request and Application - 2020-05-30 08:24:14

Fix issue with private Email initialisation - 2020-04-24 14:42:14

Vapor 4 Support - 2020-04-10 14:49:11

Fix warning from Package.swift - 2020-04-05 07:08:19

Fix internal access to the configuration properties - 2020-03-31 11:05:03

Change configuration - 2020-03-30 20:03:16

Configuration of SMTP server can be done via Application object:

app.smtp.configuration.host = "smtp.server"
app.smtp.configuration.username = "johndoe"
app.smtp.configuration.password = "passw0rd"
app.smtp.configuration.secure = .ssl

Move to Vapor4 (Swift NIO 2) - 2020-03-30 13:39:48

STARTTLS support - 2019-04-25 18:02:04

This release includes support of STARTTLS command (and further encryption).

First release - 2019-04-16 17:47:25

Features:

  • Vapor provider/service
  • SwiftNIO Support
  • Text/HTML
  • Attachments
  • SSL