def apply(): form = forms.InviteForm() if form.validate_on_submit(): full_name = form.full_name.data email = form.email.data if not models.InviteApplicant.objects.filter(email=email).first(): applicant = models.InviteApplicant(full_name=full_name, email=email) applicant.save() token = _generate_token(email) confirmation_url = "%s/confirm-email/%s" % (app.config['BASE_URL'], token) html = render_template('confirm_email.html', full_name=full_name, confirmation_url=confirmation_url) msg = Message(html=html, subject="Your application for an idealgov login", sender="*****@*****.**", recipients=[email]) try: mail.send(msg) message = "Thanks. You'll be getting a confirmation email soon at: %s." % email flash(message) return render_template('done.html', message=message) except Exception as ex: log_traceback(current_app.logger, ex) applicant.delete() flash("We weren't able to handle your request", 'error') else: flash("We've already had an application from : %s." % email) return redirect(url_for('apply')) return render_template('invite.html', form=form)
def main(): if request.method == "POST": if request.form["name"] == "": flash("Please enter a valid name.") if request.form["email"] == "": flash("Please enter a valid e-mail-address.") elif request.form["text"] == "": flash("Please enter a valid message.") else: msg = Message( "Contact form input", sender=(request.form["name"], request.form["email"]), recipients=[www.config["WWW_CONTACT_MAIL"]], body=request.form["text"], ) mail.send(msg) flash("Message sent.") return redirect(url_for("home")) return render_template("contact.html")
def invite_user(): email = request.args.get('email') full_name = request.args.get('full_name') if not (email or full_name): current_app.logger.info('must provide email and full name') return 400, 'Bad Request' www_id = app.config['WWW_CLIENT_ID'] www_key = app.config['WWW_CLIENT_KEY'] to_sign = '%s:%s:%s' % (www_id, email, full_name) signer = TimestampSigner(www_key, digest_method=hashlib.sha256) signed = signer.sign(to_sign) headers = { 'Authorisation': signed } url = '%s/register-user' % app.config['REGISTRY_BASE_URL'] resp = requests.post(url, data={'email':email, 'full_name': full_name}, headers=headers) if resp.status_code == 201: user = models.InviteApplicant.objects.filter(email=email).first() user.invited = True user.save() token = _generate_token(email) confirmation_url = "%s/confirm-account/%s" % (app.config['BASE_URL'], token) html = render_template('confirm_account.html', full_name=full_name, confirmation_url=confirmation_url) msg = Message(html=html, subject="Your idealgov account", sender="*****@*****.**", recipients=[email]) try: mail.send(msg) flash("User account created. Invite sent to: %s." % email) except Exception as ex: log_traceback(current_app.logger, ex) flash("Failed to send invite to: %s" % email, 'error') else: flash('Error creating account', 'error') return redirect(url_for('the_hatch'))
def send_ansyc_email(app, msg): with app.app_context(): mail.send(msg)