Hello, I have been using the smtp library lately and i noticed that it does not natively support html emails. So i did so research myself and in short i ended up adding some headers to the smtp request. The html mail gets sent but some html tags are not rendered like img, svg and a tags, so does anyone have a work around on how to send html mail that will in fact render appropriately using the nim smtp library. This is the code
proc sendRecoveryMail*(username, domain: string) : Future[bool] {.async.} =
let logger = newConsoleLogger()
logger.log(lvlInfo, "Sending password reset link as requested by user $1".format([username]))
try:
let
userinfo = db.getRow(sql"SELECT email, userid FROM user WHERE username = ? OR email = ?;", username, username)
sitemailinfo = db.getRow(sql"SELECT mailaddress, mailusername, mailpassword FROM site WHERE id = 1;")
if sitemailinfo[0].isEmptyOrWhitespace or sitemailinfo[1].isEmptyOrWhitespace or sitemailinfo[2].isEmptyOrWhitespace:
logger.log(lvlInfo, "Some mail info are unset")
return false
randomize()
var mailhtml = mailhtml(username, $(parseUri(domain) / "resetpassword" / $((username & $epochTime() & $rand(9999)).toMD5) / userinfo[1]))
let
mail = createMessage(
"PassWord Recovery Mail Movestuff",
mailhtml,
@[userinfo[0]],
@[sitemailinfo[0]],
otherHeaders = @[
("MIME-Version", "1.0"),
("Content-Type", "text/html; charset=\"utf-8\""),
("Content-Transfer-Encoding", "quoted-printable")
])
smtpConn = newAsyncSmtp(useSsl = true, debug=true)
await smtpConn.connect("smtp.gmail.com", Port 465)
await smtpConn.auth(sitemailinfo[1], sitemailinfo[2])
await smtpConn.sendmail(sitemailinfo[0], @[userinfo[0]], $mail)
await smtpConn.close()
logger.log(lvlInfo, "Password reset mail sent to $1".format([userinfo[0]]))
return true
except:
logger.log(lvlError, "Failed to send reset mail")
return false
For anyone still having problems sending html using the smtp library here is a code snippet to help you out
import smtp, htmlgen
let
smtpConn = newSmtp(useSsl = true, debug=true)
body = html(
body(
p("Hello there just testing out this html mail")
),
a(href = "twitter.com", "twitter link")
)
smtpConn.connect("smtp.gmail.com", Port 465)
smtpConn.auth("fooboss", "application password")
smtpConn.sendmail(
"[email protected]", @["[email protected]"],
$(createMessage(
"Test smtp mail",
body, @["[email protected]"], @[],
otherHeaders = [
(name : "Mime-Version", value : "1.0"),
(name : "Content-Type", value : "text/html; charset=\"ISO-8859-1\""),
(name : "Content-Transfer-Encoding", value : "7bit")
])
)
)
This old article helped