diff --git a/example_config.py b/example_config.py index 993e0de..e7445da 100644 --- a/example_config.py +++ b/example_config.py @@ -43,10 +43,11 @@ CONTACT_PLACEHOLDER_EMAIL="Your Email" CONTACT_PLACEHOLDER_SUBJECT="Subject" CONTACT_PLACEHOLDER_TEXTAREA="Your Message" -TARGET_SMTP=None -TARGET_EMAIL=None -TARGET_SMTP_USER=None -TARGET_SMTP_AUTH=None +TARGET_SMTP = None +TARGET_EMAIL = None +TARGET_SMTP_USER = None +TARGET_SMTP_PASSWORD = None +SMTP_MUST_BE_CONNECTED = False THANKS_TITLE = "Thank you for something!" THANKS_STRONG_TEXT = "Strong Text" diff --git a/server.py b/server.py index 3e3a2db..a5fa9bf 100755 --- a/server.py +++ b/server.py @@ -10,7 +10,6 @@ import caldav import datetime as dt import markdown2 import PIL.Image -import smtplib # sitemap utilities from werkzeug.routing import BuildError @@ -42,6 +41,7 @@ PRIORITY_SECONDARY = 0.8 # other HTTP_NOT_FOUND = 404 +HTTP_NO_CONTENT = 204 EMPTY_STRING = "" READ = "r" WRITE = "w" @@ -408,22 +408,20 @@ def init(): else: print("Warning: Subpage Config File not found", file=sys.stderr) + ## check if SMTP server is availiable ## + smtp.checkSMTPConnection(app) + + @app.route("/contact") def contact(): return flask.render_template("contact.html", conf=app.config) @app.route("/contact-api", methods=['POST']) def contactAPI(): - - email = flask.request.form["email"] - name = flask.request.form["name"] - subject = "Subject: {} ({})\n\n".format(flask.request.form["subject"], name) - message = subject + flask.request.form["message"] - smtpTarget = smtplib.SMTP(app.config["TARGET_SMTP"]) - smtpTarget.sendmail(email, app.config["TARGET_EMAIL"] , message) - smtpTarget.quit() + '''Form Location for Contact Form to be submitted to target email''' - return flask.redirect("/thanks") + smtp.sendMailFromHtmlForm(app, flask.request.form) + return (EMPTY_STRING, HTTP_NO_CONTENT) if __name__ == "__main__": diff --git a/smtp.py b/smtp.py new file mode 100644 index 0000000..254387c --- /dev/null +++ b/smtp.py @@ -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() diff --git a/static/contact.js b/static/contact.js new file mode 100644 index 0000000..1779a22 --- /dev/null +++ b/static/contact.js @@ -0,0 +1,35 @@ +function submitForm(){ + + /* show the waiting dialog */ + dialog = document.getElementById("waiting-dialog") + dialog.style.disply = "block" + setMainBackgroundOpacity(0.5) + + /* submit the form */ + xhr = new XMLHttpRequest(); + xhr.open("POST", "/your/url/name.php"); + xhr.onload = + formData = new FormData(document.getElementById("contact-form")); + xhr.send(formData); + + mainContainer = document.getElementById("main-container") + mainContainer.style.opacity = 0.5 + + window.location.href = "/thanks" + // after x seconds forward to thx + +} + +function formSubmitFinished(event){ + if(event.target.status != 200){ + showErrorMessage(); // blocking + setMainBackgroundOpacity(0.5) + }else{ + window.location.href = "/thanks" + } +} + +function setMainBackgroundOpacity(opacity){ + mainContainer = document.getElementById("main-container") + mainContainer.style.opacity = opacity +} diff --git a/templates/contact.html b/templates/contact.html index 14652e7..882854f 100644 --- a/templates/contact.html +++ b/templates/contact.html @@ -12,6 +12,7 @@ {% include 'navbar.html' %} + {% include 'progress_window.html' %}

{{ conf['CONTACT_HEADLINE'] }} diff --git a/templates/navbar.html b/templates/navbar.html index 8771b5f..5eabd0e 100644 --- a/templates/navbar.html +++ b/templates/navbar.html @@ -1,4 +1,8 @@