let email = try! 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
...
}
Using SMPT client (async/await)
You have to set macOS 12 as a target in Package.swift file (and tool version 5.5).
platforms: [
.macOS(.v12)
],
Then you can use async/await alternatives of SMTP methods.
let email = try! 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.")
try await request.smtp.send(email)
Also you can send emails directly via application class.
try await app.smtp.send(email)
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.
License
This project is licensed under the terms of the MIT license.
Smtp
This framework has dependencies only to
Vapor
andSwiftNIO
packages.SwiftNIO
support was inspired by Apple examples: Swift NIO examples.Features:
Getting started
You need to add library to
Package.swift
file:add package to dependencies:
and add product to your target:
Set the SMTP server configuration (e.g. in
main.swift
file)Using SMTP client (EventLoopFuture)
Also you can send emails directly via
application
class.Using SMPT client (async/await)
You have to set macOS 12 as a target in
Package.swift
file (and tool version 5.5).Then you can use async/await alternatives of SMTP methods.
Also you can send emails directly via
application
class.Troubleshoots
You can use
logHandler
to handle and print all messages send/retrieved from email server.Developing
After cloning the repository you can open it in Xcode.
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.License
This project is licensed under the terms of the MIT license.