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(
mailer: Mailer,
subject, message, recipient: string,
fromAddr = "forum@nim-lang.org",
otherHeaders:seq[(string, string)] = @[]
) {.async.} =
# Ensure we aren't emailing this address too much.
@ -41,6 +40,9 @@ proc sendMail(
if mailer.config.smtpAddress.len == 0:
warn("Cannot send mail: no smtp server configured (smtpAddress).")
return
if mailer.config.smtpFromAddr.len == 0:
warn("Cannot send mail: no smtp from address configured (smtpFromAddr).")
return
var client = newAsyncSmtp()
await client.connect(mailer.config.smtpAddress, Port(mailer.config.smtpPort))
@ -50,12 +52,12 @@ proc sendMail(
let toList = @[recipient]
var headers = otherHeaders
headers.add(("From", fromAddr))
headers.add(("From", mailer.config.smtpFromAddr))
let encoded = createMessage(subject, message,
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.} =
let message = """Hello $1,
@ -133,4 +135,4 @@ proc sendSecureEmail*(
if emailSentFut.error of ForumError:
raise emailSentFut.error
else:
raise newForumError("Couldn't send email", @["email"])
raise newForumError("Couldn't send email", @["email"])

View file

@ -226,7 +226,7 @@ proc initialiseDb(admin: tuple[username, password, email: string],
proc initialiseConfig(
name, title, hostname: string,
recaptcha: tuple[siteKey, secretKey: string],
smtp: tuple[address, user, password: string],
smtp: tuple[address, user, password, fromAddr: string],
isDev: bool,
dbPath: string,
ga: string=""
@ -242,6 +242,7 @@ proc initialiseConfig(
"smtpAddress": %smtp.address,
"smtpUser": %smtp.user,
"smtpPassword": %smtp.password,
"smtpFromAddr": %smtp.fromAddr,
"isDev": %isDev,
"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 smtpUser = question("SMTP user: ")
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 " &
"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"
initialiseConfig(
name, title, hostname, (recaptchaSiteKey, recaptchaSecretKey),
(smtpAddress, smtpUser, smtpPassword), isDev=false,
(smtpAddress, smtpUser, smtpPassword, smtpFromAddr), isDev=false,
dbPath, ga
)
@ -328,7 +330,7 @@ when isMainModule:
"Development Forum",
"localhost",
recaptcha=("", ""),
smtp=("", "", ""),
smtp=("", "", "", ""),
isDev=true,
dbPath
)
@ -345,7 +347,7 @@ when isMainModule:
"Test Forum",
"localhost",
recaptcha=("", ""),
smtp=("", "", ""),
smtp=("", "", "", ""),
isDev=true,
dbPath
)

View file

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