Use Reply-To in contact form

This commit is contained in:
Noah 2014-11-02 17:43:36 -08:00
parent 1f92d2a35d
commit a1b2550c0b
2 changed files with 18 additions and 7 deletions

View File

@ -38,9 +38,15 @@ def send():
flash("The message is required.") flash("The message is required.")
return redirect(url_for(".index")) return redirect(url_for(".index"))
# Email looks valid?
reply_to = None
if "@" in email and "." in email:
reply_to = email
# Send the e-mail. # Send the e-mail.
send_email( send_email(
to=NOTIFY_ADDRESS, to=NOTIFY_ADDRESS,
reply_to=reply_to,
subject="Contact Form on {}: {}".format(SITE_NAME, subject), subject="Contact Form on {}: {}".format(SITE_NAME, subject),
message="""A visitor to {site_name} has sent you a message! message="""A visitor to {site_name} has sent you a message!

View File

@ -163,7 +163,7 @@ def render_markdown(body, html_escape=True, extensions=None, blacklist=None):
return markdown.markdown(body, **args) return markdown.markdown(body, **args)
def send_email(to, subject, message, sender=None): def send_email(to, subject, message, sender=None, reply_to=None):
"""Send an e-mail out.""" """Send an e-mail out."""
if sender is None: if sender is None:
sender = MAIL_SENDER sender = MAIL_SENDER
@ -175,13 +175,18 @@ def send_email(to, subject, message, sender=None):
if MAIL_METHOD == "smtp": if MAIL_METHOD == "smtp":
# Send mail with SMTP. # Send mail with SMTP.
for email in to: for email in to:
server = smtplib.SMTP(MAIL_SERVER, MAIL_PORT) # Construct the mail headers.
server.set_debuglevel(1) headers = [
msg = """From: {} "From: {}".format(sender),
To: {} "To: {}".format(email),
Subject: {} ]
if reply_to is not None:
headers.append("Reply-To: {}".format(reply_to))
headers.append("Subject: {}".format(subject))
{}""".format(sender, email, subject, message) # Prepare the mail for transport.
server = smtplib.SMTP(MAIL_SERVER, MAIL_PORT)
msg = "\n".join(headers) + "\n\n" + message
server.sendmail(sender, email, msg) server.sendmail(sender, email, msg)
server.quit() server.quit()