From 3b6e7363a96817d945de1f27a15a8ff02b270be4 Mon Sep 17 00:00:00 2001 From: Danil Yarantsev <21169548+Yardanico@users.noreply.github.com> Date: Sun, 10 May 2020 12:52:14 +0300 Subject: [PATCH 1/3] Add ability to enable TLS for SMTP --- src/email.nim | 2 ++ src/setup_nimforum.nim | 10 ++++++---- src/utils.nim | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/email.nim b/src/email.nim index d26f4ad..fb9d5a3 100644 --- a/src/email.nim +++ b/src/email.nim @@ -45,6 +45,8 @@ proc sendMail( return var client = newAsyncSmtp() + if mailer.config.smtpTls: + await client.startTls() await client.connect(mailer.config.smtpAddress, Port(mailer.config.smtpPort)) if mailer.config.smtpUser.len > 0: await client.auth(mailer.config.smtpUser, mailer.config.smtpPassword) diff --git a/src/setup_nimforum.nim b/src/setup_nimforum.nim index e50b96d..c1f8473 100644 --- a/src/setup_nimforum.nim +++ b/src/setup_nimforum.nim @@ -234,7 +234,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, fromAddr: string], + smtp: tuple[address, user, password, fromAddr: string, tls: bool], isDev: bool, dbPath: string, ga: string="" @@ -251,6 +251,7 @@ proc initialiseConfig( "smtpUser": %smtp.user, "smtpPassword": %smtp.password, "smtpFromAddr": %smtp.fromAddr, + "smtpTls": %smtp.tls, "isDev": %isDev, "dbPath": %dbPath } @@ -294,6 +295,7 @@ These can be changed later in the generated forum.json file. let smtpUser = question("SMTP user: ") let smtpPassword = readPasswordFromStdin("SMTP pass: ") let smtpFromAddr = question("SMTP sending email address (eg: mail@mail.hostname.com): ") + let smtpTls = parseBool(question("Enable TLS for SMTP: ")) echo("The following is optional. You can specify your Google Analytics ID " & "if you wish. Otherwise just leave it blank.") @@ -303,7 +305,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, smtpFromAddr), isDev=false, + (smtpAddress, smtpUser, smtpPassword, smtpFromAddr, smtpTls), isDev=false, dbPath, ga ) @@ -338,7 +340,7 @@ when isMainModule: "Development Forum", "localhost", recaptcha=("", ""), - smtp=("", "", "", ""), + smtp=("", "", "", "", false), isDev=true, dbPath ) @@ -355,7 +357,7 @@ when isMainModule: "Test Forum", "localhost", recaptcha=("", ""), - smtp=("", "", "", ""), + smtp=("", "", "", "", false), isDev=true, dbPath ) diff --git a/src/utils.nim b/src/utils.nim index 1be058b..b915a41 100644 --- a/src/utils.nim +++ b/src/utils.nim @@ -17,6 +17,7 @@ type smtpUser*: string smtpPassword*: string smtpFromAddr*: string + smtpTls*: bool mlistAddress*: string recaptchaSecretKey*: string recaptchaSiteKey*: string From 6c6ed08ec96368e05143bec15e693d370a0e986c Mon Sep 17 00:00:00 2001 From: Danil Yarantsev <21169548+Yardanico@users.noreply.github.com> Date: Sun, 10 May 2020 12:56:13 +0300 Subject: [PATCH 2/3] startTls should be after connection of course --- src/email.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/email.nim b/src/email.nim index fb9d5a3..f89f010 100644 --- a/src/email.nim +++ b/src/email.nim @@ -45,9 +45,9 @@ proc sendMail( return var client = newAsyncSmtp() + await client.connect(mailer.config.smtpAddress, Port(mailer.config.smtpPort)) if mailer.config.smtpTls: await client.startTls() - await client.connect(mailer.config.smtpAddress, Port(mailer.config.smtpPort)) if mailer.config.smtpUser.len > 0: await client.auth(mailer.config.smtpUser, mailer.config.smtpPassword) From a3052efd781dc24b9d896fa70d9f477e05c8a2ae Mon Sep 17 00:00:00 2001 From: Danil Yarantsev <21169548+Yardanico@users.noreply.github.com> Date: Sun, 10 May 2020 12:59:31 +0300 Subject: [PATCH 3/3] Add smptTls to config loading --- src/utils.nim | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils.nim b/src/utils.nim index b915a41..5c71b28 100644 --- a/src/utils.nim +++ b/src/utils.nim @@ -56,6 +56,7 @@ proc loadConfig*(filename = getCurrentDir() / "forum.json"): Config = result.smtpUser = root{"smtpUser"}.getStr("") result.smtpPassword = root{"smtpPassword"}.getStr("") result.smtpFromAddr = root{"smtpFromAddr"}.getStr("") + result.smtpTls = root{"smtpTls"}.getBool(false) result.mlistAddress = root{"mlistAddress"}.getStr("") result.recaptchaSecretKey = root{"recaptchaSecretKey"}.getStr("") result.recaptchaSiteKey = root{"recaptchaSiteKey"}.getStr("")