Skip to content

Commit

Permalink
Added contact form email function
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianLutzCL committed Apr 27, 2019
1 parent 456aace commit 07da694
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions monitor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
from monitor.models import CheckedWebsite, updateDatabase
from sqlalchemy import desc

import smtplib
import os

EMAIL_ADRESS = os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')


@app.route("/", methods=['GET', 'POST'])
def index():
Expand All @@ -18,14 +24,59 @@ def index():
database_query_update = CheckedWebsite.query.order_by(desc(CheckedWebsite.check_date)).limit(10).all()
return render_template('index.html', title="Is the website down? | ServerMonitor", url=url, status_code=status_code, status_reason=status_reason, ip=server_ip, lat=server_latency, loc=server_location, isdown=isdown, database_query=database_query_update)
else:
return render_template('index.html', title="Is the website down? | ServerMonitor", url='url', status_code='status_code', status_reason='status_reason', lat='-', loc='-', ip='-', isdown='isdown', database_query=database_query)
database_query_last = CheckedWebsite.query.order_by(desc(CheckedWebsite.check_date)).first() #CheckedWebsite.query.order_by(desc(CheckedWebsite.check_date)).limit(1).first()
database_query_last_str = str(database_query_last)
database_query_last_list = database_query_last_str.split()

check_date = str(database_query_last_list[0] + " " + database_query_last_list[1])
url = str(database_query_last_list[2])
status_code = database_query_last_list[3]

if status_code != 'NONE':
status_code = int(database_query_last_list[3])
else:
status_code = 'NONE'

status_reason = str(database_query_last_list[4])
isdown = database_query_last_list[5]

if isdown == 'True':
isdown = True
else:
isdown = False

return render_template('index.html', title="Is the website down? | ServerMonitor", url=url, status_code=status_code, status_reason=status_reason, lat='-', loc='-', ip='-', isdown=isdown, database_query=database_query)


@app.route("/info", methods=('GET', 'POST'))
def info():
return render_template('info.html', title="Info | ServerMonitor")
if request.method == 'POST':
name = request.form['txtName']
email = request.form['txtEmail']
website = request.form['txtWebsite']
message = request.form['txtMsg']
mail(name, email, website, message)
return render_template('info.html', title="SENT | ServerMonitor")
else:
return render_template('info.html', title="Info | ServerMonitor")


@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404



def mail(name, email, website, message):
with smtplib.SMTP('smtp.googlemail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()

smtp.login(EMAIL_ADRESS, EMAIL_PASSWORD)

subject = 'InspiredProgrammer ServerMonitor - Contact Form'
body = 'Mail from ' + name + ', ' + email + ' (' + website + ')\n\n' + message
msg = f'Subject: {subject}\n\n{body}'

smtp.sendmail(EMAIL_ADRESS, EMAIL_ADRESS, msg)

0 comments on commit 07da694

Please sign in to comment.