flask 邮件发送页面(flask 32)

subscribe.html

{% extends 'base.html' %}
{% from 'macros.html' import form_field %}

{% block content %}

Flask Weekly


A free weekly newsletter featuring curated news, articles, new releases, jobs etc related to Flask.


Delivery at every Wednesday morning.


Unsubscribe at any time.



{{ form.csrf_token }}
{{ form_field(form.name, placeholder='Your Name') }}
{{ form_field(form.email, placeholder='Your Email') }}

{{ form.submit(class='btn') }}

{% endblock %}

app.py

from flask import Flask,render_template,flash,redirect,url_for
import os
from flask_mail import Mail, Message
from wtforms import StringField, TextAreaField, SubmitField
from wtforms.validators import DataRequired, Email
from flask_wtf import FlaskForm

app = Flask(name)
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

app.config.update(
SECRET_KEY=os.getenv('SECRET_KEY', 'secret string'),
MAIL_SERVER=os.getenv('MAIL_SERVER'),
MAIL_PORT=465,
MAIL_USE_SSL=True,
MAIL_USERNAME=os.getenv('MAIL_USERNAME'),
MAIL_PASSWORD=os.getenv('MAIL_PASSWORD'),
MAIL_DEFAULT_SENDER=('ssssssss', os.getenv('MAIL_USERNAME'))
)

mail = Mail(app)
def send_smtp_mail(subject, to, body):
message = Message(subject, recipients=[to], body=body)
mail.send(message)
class SubscribeForm(FlaskForm):
name = StringField('Name', validators=[DataRequired()])
email = StringField('Email', validators=[DataRequired(), Email()])
submit = SubmitField('Subscribe')
@app.route('/subscribe', methods=['GET', 'POST'])
def subscribe():
form = SubscribeForm()
if form.validate_on_submit():
name = form.name.data
email = form.email.data
send_smtp_mail('Subscribe Success!', email, name=name)
flash('Confirmation email have been sent! Check your inbox.')
return redirect(url_for('subscribe'))
return render_template('subscribe.html', form=form)
if name == 'main':
app.run()

你可能感兴趣的:(flask 邮件发送页面(flask 32))