diff --git a/rophako/modules/contact/__init__.py b/rophako/modules/contact/__init__.py index 838db48..6b52608 100644 --- a/rophako/modules/contact/__init__.py +++ b/rophako/modules/contact/__init__.py @@ -38,9 +38,15 @@ def send(): flash("The message is required.") 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_email( to=NOTIFY_ADDRESS, + reply_to=reply_to, subject="Contact Form on {}: {}".format(SITE_NAME, subject), message="""A visitor to {site_name} has sent you a message! diff --git a/rophako/utils.py b/rophako/utils.py index d40adc6..9268e34 100644 --- a/rophako/utils.py +++ b/rophako/utils.py @@ -163,7 +163,7 @@ def render_markdown(body, html_escape=True, extensions=None, blacklist=None): 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.""" if sender is None: sender = MAIL_SENDER @@ -175,13 +175,18 @@ def send_email(to, subject, message, sender=None): if MAIL_METHOD == "smtp": # Send mail with SMTP. for email in to: - server = smtplib.SMTP(MAIL_SERVER, MAIL_PORT) - server.set_debuglevel(1) - msg = """From: {} -To: {} -Subject: {} + # Construct the mail headers. + headers = [ + "From: {}".format(sender), + "To: {}".format(email), + ] + 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.quit()