Browse Source

Use Reply-To in contact form

pull/2/head
Noah Petherbridge 5 years ago
parent
commit
a1b2550c0b
2 changed files with 18 additions and 7 deletions
  1. +6
    -0
      rophako/modules/contact/__init__.py
  2. +12
    -7
      rophako/utils.py

+ 6
- 0
rophako/modules/contact/__init__.py View File

@@ -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!



+ 12
- 7
rophako/utils.py View File

@@ -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:
# 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))

# Prepare the mail for transport.
server = smtplib.SMTP(MAIL_SERVER, MAIL_PORT)
server.set_debuglevel(1)
msg = """From: {}
To: {}
Subject: {}

{}""".format(sender, email, subject, message)
msg = "\n".join(headers) + "\n\n" + message
server.sendmail(sender, email, msg)
server.quit()



Loading…
Cancel
Save