diff --git a/README.md b/README.md index cd974d5..5850fdf 100644 --- a/README.md +++ b/README.md @@ -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: "" - ) - } -} +``` +SMTP_SERVER=smtp.example.com +SMTP_ACCOUNT=noreply@example.com +SMTP_PASSWORD=SECRET ``` ### Sending a Mail @@ -44,7 +36,7 @@ app.get { request -> EventLoopFuture 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!" } } diff --git a/Sources/VaporSMTPKit/VaporSMTPKit.swift b/Sources/VaporSMTPKit/VaporSMTPKit.swift index 4208c60..f4a29cb 100644 --- a/Sources/VaporSMTPKit/VaporSMTPKit.swift +++ b/Sources/VaporSMTPKit/VaporSMTPKit.swift @@ -4,7 +4,7 @@ import SMTPKitten extension Application { public func sendMail( _ mail: Mail, - withCredentials credentials: SMTPCredentials, + withCredentials credentials: SMTPCredentials = Application.defaultSMTPCredentials, preventedDomains: Set = ["example.com"] ) -> EventLoopFuture { return sendMails([mail], withCredentials: credentials, preventedDomains: preventedDomains) @@ -12,7 +12,7 @@ extension Application { public func sendMails( _ mails: [Mail], - withCredentials credentials: SMTPCredentials, + withCredentials credentials: SMTPCredentials = Application.defaultSMTPCredentials, preventedDomains: Set = ["example.com"] ) -> EventLoopFuture { func filterMailAddress(_ address: MailUser) -> Bool { @@ -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") ?? "" + ) + } +}