Commit 51dee7be authored by buttle's avatar buttle

Added starttls (may work) to smtp config.

Added check for available email
parent a03e42fc
......@@ -21,7 +21,7 @@ from flask import g, flash, request
from flask_babel import gettext
from GNGforms import app
from GNGforms.persistence import Site
import smtplib, socket
import smtplib, ssl, socket
from threading import Thread
def createSmtpObj():
......@@ -30,10 +30,18 @@ def createSmtpObj():
if config["encryption"] == "SSL":
server = smtplib.SMTP_SSL(config["host"], port=config["port"], timeout=2)
server.login(config["user"], config["password"])
elif config["encryption"] == "STARTTLS":
server = smtplib.SMTP_SSL(config["host"], port=config["port"], timeout=2)
context = ssl.create_default_context()
server.starttls(context=context)
server.login(config["user"], config["password"])
else:
server = smtplib.SMTP(config["host"], port=config["port"])
if config["user"] and config["password"]:
server.login(config["user"], config["password"])
return server
except socket.error as e:
if g.isAdmin:
......@@ -55,7 +63,7 @@ def sendMail(email, message):
return False
def smtpSendConfirmEmail(user, newEmail=None):
def sendConfirmEmail(user, newEmail=None):
link="%suser/validate-email/%s" % (Site().host_url, user.token['token'])
message=gettext("Hello %s\n\nPlease confirm your email\n\n%s") % (user.username, link)
message = 'Subject: {}\n\n{}'.format(gettext("GNGforms. Confirm email"), message)
......@@ -65,7 +73,7 @@ def smtpSendConfirmEmail(user, newEmail=None):
return sendMail(user.email, message)
def smtpSendInvite(invite):
def sendInvite(invite):
site=Site(hostname=invite.data['hostname'])
link="%suser/new/%s" % (site.host_url, invite.data['token']['token'])
message="%s\n\n%s" % (invite.data['message'], link)
......@@ -74,7 +82,7 @@ def smtpSendInvite(invite):
return sendMail(invite.data['email'], message)
def smtpSendRecoverPassword(user):
def sendRecoverPassword(user):
link="%ssite/recover-password/%s" % (Site().host_url, user.token['token'])
message=gettext("Please use this link to recover your password")
message="%s\n\n%s" % (message, link)
......@@ -83,7 +91,7 @@ def smtpSendRecoverPassword(user):
return sendMail(user.email, message)
def smtpSendNewFormEntryNotification(emails, entry, slug):
def sendNewFormEntryNotification(emails, entry, slug):
message=gettext("New form entry in %s at %s\n" % (slug, Site().hostname))
for data in entry:
message="%s\n%s: %s" % (message, data[0], data[1])
......@@ -93,21 +101,21 @@ def smtpSendNewFormEntryNotification(emails, entry, slug):
for email in emails:
sendMail(email, message)
def smtpSendExpiredFormNotification(editorEmails, form):
def sendExpiredFormNotification(editorEmails, form):
message=gettext("The form '%s' has expired at %s" % (form.slug, Site().hostname))
message='Subject: {}\n\n{}'.format(gettext("GNGforms. A form has expired"), message)
for email in editorEmails:
sendMail(email, message)
def smtpSendNewFormNotification(adminEmails, form):
def sendNewFormNotification(adminEmails, form):
message=gettext("New form '%s' created at %s" % (form.slug, Site().hostname))
message='Subject: {}\n\n{}'.format(gettext("GNGforms. New form notification"), message)
for email in adminEmails:
sendMail(email, message)
def smtpSendNewUserNotification(adminEmails, username):
def sendNewUserNotification(adminEmails, username):
message=gettext("New user '%s' created at %s" % (username, Site().hostname))
message='Subject: {}\n\n{}'.format(gettext("GNGforms. New user notification"), message)
......@@ -115,7 +123,7 @@ def smtpSendNewUserNotification(adminEmails, username):
sendMail(email, message)
def smtpSendTestEmail(email):
def sendTestEmail(email):
message=gettext("Congratulations!")
message='Subject: {}\n\n{}'.format(gettext("SMTP test"), message)
......
......@@ -139,6 +139,9 @@ class User(object):
if User(email=email):
flash(gettext("Email address is not available"), 'warning')
return False
if email in app.config['ROOT_USERS'] and Installation.isUser(email):
flash(gettext("Email address is not available"), 'warning')
return False
return True
......
......@@ -31,6 +31,7 @@ label { margin-top:0.25em }
<select type="number" name="encryption" class="form-control" value="{{encryption}}" >
<option class="form-control" name="hello" {% if not encryption == "SSL" %}selected=True{% endif %} >{%trans%}None{%endtrans%}</option>
<option class="form-control" name="SSL" {% if encryption == "SSL" %}selected=True{% endif %} >SSL</option>
<option class="form-control" name="STARTTLS" {% if encryption == "STARTTLS" %}selected=True{% endif %} >STARTTLS (may work)</option>
</select>
<label>{%trans%}User{%endtrans%}</label>
......
......@@ -26,7 +26,7 @@ from flask_babel import gettext, refresh
from GNGforms.persistence import *
from GNGforms.session import *
from GNGforms.utils import *
from GNGforms.email import *
import GNGforms.email as smtp
from form_templates import formTemplates
import pprint
......@@ -129,7 +129,7 @@ def view_form(slug):
emails.append(user.email)
if emails:
def sendExpiredFormNotification():
smtpSendExpiredFormNotification(emails, queriedForm)
smtp.sendExpiredFormNotification(emails, queriedForm)
thread = Thread(target=sendExpiredFormNotification())
thread.start()
queriedForm.save()
......@@ -146,7 +146,7 @@ def view_form(slug):
for field in queriedForm.fieldIndex:
if field['name'] in entry:
data.append( (stripHTMLTagsForLabel(field['label']), entry[field['name']]) )
smtpSendNewFormEntryNotification(emails, data, queriedForm.slug)
smtp.sendNewFormEntryNotification(emails, data, queriedForm.slug)
thread = Thread(target=sendEntryNotification())
thread.start()
return render_template('thankyou.html', form=queriedForm)
......@@ -536,7 +536,7 @@ def save_form(_id=None):
newForm.addLog(gettext("Form created"))
flash(gettext("Saved form OK"), 'success')
# notify Admins
smtpSendNewFormNotification(User().getNotifyNewFormEmails(), newForm)
smtp.sendNewFormNotification(User().getNotifyNewFormEmails(), newForm)
return redirect(make_url_for('inspect_form', _id=newForm._id))
clearSessionFormData()
......@@ -811,7 +811,7 @@ def change_email():
if 'email' in request.form and isValidEmail(request.form['email']):
g.current_user.setToken(email=request.form['email'])
smtpSendConfirmEmail(g.current_user, request.form['email'])
smtp.sendConfirmEmail(g.current_user, request.form['email'])
flash(gettext("We've sent an email to %s") % request.form['email'], 'info')
return redirect(make_url_for('user_settings', username=g.current_user.username))
......@@ -822,7 +822,7 @@ def change_email():
@login_required
def send_validation_email():
g.current_user.setToken(email=g.current_user.email)
smtpSendConfirmEmail(g.current_user, g.current_user.email)
smtp.sendConfirmEmail(g.current_user, g.current_user.email)
flash(gettext("We've sent an email to %s") % g.current_user.email, 'info')
return redirect(make_url_for('user_settings', username=g.current_user.username))
......@@ -902,7 +902,7 @@ def new_user(token=None):
if invite:
invite.delete()
thread = Thread(target=smtpSendNewUserNotification(User().getNotifyNewUserEmails(), user.username))
thread = Thread(target=smtp.sendNewUserNotification(User().getNotifyNewUserEmails(), user.username))
thread.start()
if validatedEmail == True:
......@@ -912,7 +912,7 @@ def new_user(token=None):
return redirect(make_url_for('my_forms'))
else:
user.setToken()
smtpSendConfirmEmail(user)
smtp.sendConfirmEmail(user)
return render_template('new-user.html', site=g.site, created=True)
session["user_id"]=None
......@@ -960,7 +960,7 @@ def recover_password(token=None):
user = User(email=request.form['email'], blocked=False)
if user:
user.setToken()
smtpSendRecoverPassword(user)
smtp.sendRecoverPassword(user)
flash(gettext("We may have sent you an email"), 'info')
if not user and request.form['email'] in app.config['ROOT_USERS']:
......@@ -1096,7 +1096,7 @@ def smtp_config():
@admin_required
def test_smtp(email):
if isValidEmail(email):
if smtpSendTestEmail(email):
if smtp.sendTestEmail(email):
flash(gettext("SMTP config works!"), 'success')
else:
flash("Email not valid", 'warning')
......@@ -1258,7 +1258,7 @@ def new_invite():
else:
message=request.form['message']
invite=Invite().create(hostname, request.form['email'], message, admin)
smtpSendInvite(invite)
smtp.sendInvite(invite)
flash(gettext("We've sent an invitation to %s") % invite.data['email'], 'success')
return redirect(make_url_for('user_settings', username=g.current_user.username))
sites=[]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment