Skip to content
Open
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
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,12 @@ In Swift 5.2 manifests, this looks like the following:
```

## Setup
Create a `.env` file in your project directory, then put in the credentials for your smtp server:

```swift
import VaporSMTPKit

extension SMTPCredentials {
static var `default`: SMTPCredentials {
return SMTPCredentials(
hostname: "smtp.example.com",
ssl: .startTLS(configuration: .default),
email: "noreply@example.com",
password: "<SECRET>"
)
}
}
```
SMTP_SERVER=smtp.example.com
SMTP_ACCOUNT=noreply@example.com
SMTP_PASSWORD=SECRET
```

### Sending a Mail
Expand All @@ -44,7 +36,7 @@ app.get { request -> EventLoopFuture<String> in
text: "You've set up mail!"
)

return request.application.sendMail(email, withCredentials: .default).map {
return request.application.sendMail(email).map {
return "Check your mail!"
}
}
Expand Down
15 changes: 13 additions & 2 deletions Sources/VaporSMTPKit/VaporSMTPKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import SMTPKitten
extension Application {
public func sendMail(
_ mail: Mail,
withCredentials credentials: SMTPCredentials,
withCredentials credentials: SMTPCredentials = Application.defaultSMTPCredentials,
preventedDomains: Set<String> = ["example.com"]
) -> EventLoopFuture<Void> {
return sendMails([mail], withCredentials: credentials, preventedDomains: preventedDomains)
}

public func sendMails(
_ mails: [Mail],
withCredentials credentials: SMTPCredentials,
withCredentials credentials: SMTPCredentials = Application.defaultSMTPCredentials,
preventedDomains: Set<String> = ["example.com"]
) -> EventLoopFuture<Void> {
func filterMailAddress(_ address: MailUser) -> Bool {
Expand Down Expand Up @@ -78,3 +78,14 @@ public struct SMTPCredentials {
self.password = password
}
}

extension Application {
public static var defaultSMTPCredentials: SMTPCredentials {
return SMTPCredentials(
hostname: Environment.get("SMTP_SERVER") ?? "smtp.example.com",
ssl: .startTLS(configuration: .default),
email: Environment.get("SMTP_ACCOUNT") ?? "noreply@example.com",
password: Environment.get("SMTP_PASSWORD") ?? "<SECRET>"
)
}
}