mirror of
https://github.com/FAUSheppy/flask-json-dream-website
synced 2025-12-06 00:01:36 +01:00
implement basic functionality of contact via form->smtp
This commit is contained in:
33
smtp.py
Normal file
33
smtp.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import sys
|
||||
import smtplib
|
||||
import smtplib.SMTPException
|
||||
|
||||
def checkSMTPConnection(app):
|
||||
'''Check connection and login on SMTP server configured in app config'''
|
||||
|
||||
try:
|
||||
if not all([ x in app.conf for x in ["TARGET_SMTP", "TARGET_EMAIL"]:
|
||||
raise smtplib.SMTPException("Missing Configuration.")
|
||||
smtpTarget = smtplib.SMTP(app.config["TARGET_SMTP"])
|
||||
if app.config["TARGET_SMTP_USER"] and app.config["TARGET_SMTP_PASSWORD"]:
|
||||
smtp.login(app.config["TARGET_SMTP_USER"], app.config["TARGET_SMTP_PASSWORD"])
|
||||
smtpTarget.quit()
|
||||
except smtplib.SMTPException as e:
|
||||
if app.config["STMP_MUST_BE_CONNECTED"]:
|
||||
print(e, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
else:
|
||||
print("Warning: SMTP unusable: {}".format(e), file=sys.stderr)
|
||||
|
||||
def sendMailFromHtmlForm(app, htmlForm):
|
||||
'''Take the app config and the contact HTML-form and send a mail accordingly'''
|
||||
|
||||
email = htmlForm["email"]
|
||||
name = htmlForm["name"]
|
||||
message = htmlForm["message"]
|
||||
|
||||
subject = "Subject: {} ({})\n\n".format(flask.request.form["subject"], name)
|
||||
|
||||
smtpTarget = smtplib.SMTP(app.config["TARGET_SMTP"])
|
||||
smtpTarget.sendmail(email, app.config["TARGET_EMAIL"] , message)
|
||||
smtpTarget.quit()
|
||||
Reference in New Issue
Block a user