make send mail from address configurable (#191)

* feat(email): make send mail from address configurable

* Exit sendMail early if there is no smtpFromAddr.

* Small adjustment to setup_nimforum
This commit is contained in:
Mr.Chun 2018-07-29 23:04:36 +08:00 • committed by Dominik Picheta
commit 7e42479228
3 changed files with 14 additions and 8 deletions

View file

@ -30,7 +30,6 @@ proc rateCheck(mailer: Mailer, address: string): bool =
proc sendMail( proc sendMail(
mailer: Mailer, mailer: Mailer,
subject, message, recipient: string, subject, message, recipient: string,
fromAddr = "forum@nim-lang.org",
otherHeaders:seq[(string, string)] = @[] otherHeaders:seq[(string, string)] = @[]
) {.async.} = ) {.async.} =
# Ensure we aren't emailing this address too much. # Ensure we aren't emailing this address too much.
@ -41,6 +40,9 @@ proc sendMail(
if mailer.config.smtpAddress.len == 0: if mailer.config.smtpAddress.len == 0:
warn("Cannot send mail: no smtp server configured (smtpAddress).") warn("Cannot send mail: no smtp server configured (smtpAddress).")
return return
if mailer.config.smtpFromAddr.len == 0:
warn("Cannot send mail: no smtp from address configured (smtpFromAddr).")
return
var client = newAsyncSmtp() var client = newAsyncSmtp()
await client.connect(mailer.config.smtpAddress, Port(mailer.config.smtpPort)) await client.connect(mailer.config.smtpAddress, Port(mailer.config.smtpPort))
@ -50,12 +52,12 @@ proc sendMail(
let toList = @[recipient] let toList = @[recipient]
var headers = otherHeaders var headers = otherHeaders
headers.add(("From", fromAddr)) headers.add(("From", mailer.config.smtpFromAddr))
let encoded = createMessage(subject, message, let encoded = createMessage(subject, message,
toList, @[], headers) toList, @[], headers)
await client.sendMail(fromAddr, toList, $encoded) await client.sendMail(mailer.config.smtpFromAddr, toList, $encoded)
proc sendPassReset(mailer: Mailer, email, user, resetUrl: string) {.async.} = proc sendPassReset(mailer: Mailer, email, user, resetUrl: string) {.async.} =
let message = """Hello $1, let message = """Hello $1,

View file

@ -226,7 +226,7 @@ proc initialiseDb(admin: tuple[username, password, email: string],
proc initialiseConfig( proc initialiseConfig(
name, title, hostname: string, name, title, hostname: string,
recaptcha: tuple[siteKey, secretKey: string], recaptcha: tuple[siteKey, secretKey: string],
smtp: tuple[address, user, password: string], smtp: tuple[address, user, password, fromAddr: string],
isDev: bool, isDev: bool,
dbPath: string, dbPath: string,
ga: string="" ga: string=""
@ -242,6 +242,7 @@ proc initialiseConfig(
"smtpAddress": %smtp.address, "smtpAddress": %smtp.address,
"smtpUser": %smtp.user, "smtpUser": %smtp.user,
"smtpPassword": %smtp.password, "smtpPassword": %smtp.password,
"smtpFromAddr": %smtp.fromAddr,
"isDev": %isDev, "isDev": %isDev,
"dbPath": %dbPath "dbPath": %dbPath
} }
@ -284,6 +285,7 @@ These can be changed later in the generated forum.json file.
let smtpAddress = question("SMTP address (eg: mail.hostname.com): ") let smtpAddress = question("SMTP address (eg: mail.hostname.com): ")
let smtpUser = question("SMTP user: ") let smtpUser = question("SMTP user: ")
let smtpPassword = readPasswordFromStdin("SMTP pass: ") let smtpPassword = readPasswordFromStdin("SMTP pass: ")
let smtpFromAddr = question("SMTP sending email address (eg: mail@mail.hostname.com): ")
echo("The following is optional. You can specify your Google Analytics ID " & echo("The following is optional. You can specify your Google Analytics ID " &
"if you wish. Otherwise just leave it blank.") "if you wish. Otherwise just leave it blank.")
@ -293,7 +295,7 @@ These can be changed later in the generated forum.json file.
let dbPath = "nimforum.db" let dbPath = "nimforum.db"
initialiseConfig( initialiseConfig(
name, title, hostname, (recaptchaSiteKey, recaptchaSecretKey), name, title, hostname, (recaptchaSiteKey, recaptchaSecretKey),
(smtpAddress, smtpUser, smtpPassword), isDev=false, (smtpAddress, smtpUser, smtpPassword, smtpFromAddr), isDev=false,
dbPath, ga dbPath, ga
) )
@ -328,7 +330,7 @@ when isMainModule:
"Development Forum", "Development Forum",
"localhost", "localhost",
recaptcha=("", ""), recaptcha=("", ""),
smtp=("", "", ""), smtp=("", "", "", ""),
isDev=true, isDev=true,
dbPath dbPath
) )
@ -345,7 +347,7 @@ when isMainModule:
"Test Forum", "Test Forum",
"localhost", "localhost",
recaptcha=("", ""), recaptcha=("", ""),
smtp=("", "", ""), smtp=("", "", "", ""),
isDev=true, isDev=true,
dbPath dbPath
) )

View file

@ -21,6 +21,7 @@ type
smtpPort*: int smtpPort*: int
smtpUser*: string smtpUser*: string
smtpPassword*: string smtpPassword*: string
smtpFromAddr*: string
mlistAddress*: string mlistAddress*: string
recaptchaSecretKey*: string recaptchaSecretKey*: string
recaptchaSiteKey*: string recaptchaSiteKey*: string
@ -58,6 +59,7 @@ proc loadConfig*(filename = getCurrentDir() / "forum.json"): Config =
result.smtpPort = root{"smtpPort"}.getNum(25).int result.smtpPort = root{"smtpPort"}.getNum(25).int
result.smtpUser = root{"smtpUser"}.getStr("") result.smtpUser = root{"smtpUser"}.getStr("")
result.smtpPassword = root{"smtpPassword"}.getStr("") result.smtpPassword = root{"smtpPassword"}.getStr("")
result.smtpFromAddr = root{"smtpFromAddr"}.getStr("")
result.mlistAddress = root{"mlistAddress"}.getStr("") result.mlistAddress = root{"mlistAddress"}.getStr("")
result.recaptchaSecretKey = root{"recaptchaSecretKey"}.getStr("") result.recaptchaSecretKey = root{"recaptchaSecretKey"}.getStr("")
result.recaptchaSiteKey = root{"recaptchaSiteKey"}.getStr("") result.recaptchaSiteKey = root{"recaptchaSiteKey"}.getStr("")