用SendGrid smtp方式发送邮件,一切显示正常,下面是app.py:
# -*- coding: utf-8 -*-
import os
from flask import Flask, flash, redirect, url_for, render_template, request
from flask_mail import Mail, Message
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, SubmitField
from wtforms.validators import DataRequired, Email
app = Flask(__name__)
app.config.update(
SECRET_KEY=os.getenv('SECRET_KEY', 'secret string'),
MAIL_SERVER=os.getenv('MAIL_SERVER'),
MAIL_PORT=os.getenv('MAIL_PORT'),
MAIL_USE_TSL=os.getenv('MAIL_USE_TSL'),
MAIL_USERNAME=os.getenv('MAIL_USERNAME'),
MAIL_PASSWORD=os.getenv('SENDGRID_API_KEY'),
MAIL_DEFAULT_SENDER=('goldwind', os.getenv('MAIL_USERNAME'))
)
mail = Mail(app)
# send mail by SMTP
def send_smtp_mail(subject, to, body):
message = Message(subject, recipients=[to], body=body)
mail.send(message)
# Form
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 + 'Hello, thank you for subscribing Flask web!')
return redirect(url_for('index'))
return render_template('subscribe.html', form=form)
下面是.env文件:
MAIL_SERVER=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USE_TSL=True
MAIL_USERNAME=apikey
SENDGRID_API_KEY=
下面是发送邮件时的日志:
192.168.208.1 - - [08/Mar/2019 09:55:03] "GET /subscribe HTTP/1.1" 200 -
send: 'ehlo [127.0.1.1]\r\n'
reply: b'250-smtp.sendgrid.net\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-SIZE 31457280\r\n'
reply: b'250-STARTTLS\r\n'
reply: b'250-AUTH PLAIN LOGIN\r\n'
reply: b'250 AUTH=PLAIN LOGIN\r\n'
reply: retcode (250); Msg: b'smtp.sendgrid.net\n8BITMIME\nPIPELINING\nSIZE 31457280\nSTARTTLS\nAUTH PLAIN LOGIN\nAUTH=PLAIN LOGIN'
send: 'AUTH PLAIN AGFwaWtleQBTRy5mbjNBSUpVdVFRV3ZFLUNXV2FkTGRRLno3ZDZlMGlnVExicTVkWXpmMnNQVG5Yd1F3el9BV245Q09XWDdKaFBDcWM=\r\n'
reply: b'235 Authentication successful\r\n'
reply: retcode (235); Msg: b'Authentication successful'
send: 'mail FROM:<apikey> size=340\r\n'
reply: b'250 Sender address accepted\r\n'
reply: retcode (250); Msg: b'Sender address accepted'
send: 'rcpt TO:<yubaoying@foxmail.com>\r\n'
reply: b'250 Recipient address accepted\r\n'
reply: retcode (250); Msg: b'Recipient address accepted'
send: 'data\r\n'
reply: b'354 Continue\r\n'
reply: retcode (354); Msg: b'Continue'
data: (354, b'Continue')
send: b'Content-Type: text/plain; charset="utf-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\nSubject: Subscribe Success!\r\nFrom: =?utf-8?q?goldwind?= <apikey>\r\nTo: yubaoying@foxmail.com\r\nDate: Fri, 08 Mar 2019 09:55:15 +0800\r\nMessage-ID: <155201011366.4968.8553629946546010542@dev-server>\r\n\r\njjjjHello, thank you for subscribing Flask web!\r\n.\r\n'
reply: b'250 Ok: queued as zlBO4VvfR76SyV23F0MPMQ\r\n'
reply: retcode (250); Msg: b'Ok: queued as zlBO4VvfR76SyV23F0MPMQ'
data: (250, b'Ok: queued as zlBO4VvfR76SyV23F0MPMQ')
send: 'quit\r\n'
reply: b'221 See you later\r\n'
reply: retcode (221); Msg: b'See you later'
192.168.208.1 - - [08/Mar/2019 09:55:17] "POST /subscribe HTTP/1.1" 302 -
192.168.208.1 - - [08/Mar/2019 09:55:17] "GET / HTTP/1.1" 200 -
一切看着都正常,但是在邮箱里就是找不到邮件,无论是在收件箱、垃圾桶、已删除邮件。
我发送到QQ邮箱,和gmail邮箱,都收不到。