欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Flask-mail发送邮件详细步骤及出现的错误

程序员文章站 2022-07-12 15:58:20
...


  这一周的时间,有点空余都在继续学习flask web,其中flask-mail这一节,算是最大的一个坑了吧!我将一步步把大家从坑里拉出来!

一、首先,将邮箱换成163的

app.config['MAIL_SERVER'] = 'smtp.163.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True

二、设置环境变量

set MAIL_USERNAME=xxxx@163.com
set MAIL_PASSWORD=xxxx

是的,我不知道在什么地方,足足坑了我两天!

一开始,我在PyCharm的Terminal中设置,不行!!

后来,我又在cmd命令下设置,但是每回重启又不行了,呵呵!

最后,在我的电脑-右键-属性-高级系统设置-环境变量,将MAIL_USERNAME等设置

 

三、设置163邮箱POP3/SMTP/IMAP,如果不设置,将有如下错误:

Flask-mail发送邮件详细步骤及出现的错误
            
    
    博客分类: python3

 

  File "D:\PycharmProjects\hello\venv\lib\site-packages\flask_mail.py", line 165, in configure_host
    host.login(self.mail.username, self.mail.password)
  File "E:\Python36\Lib\smtplib.py", line 730, in login
    raise last_exception
  File "E:\Python36\Lib\smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "E:\Python36\Lib\smtplib.py", line 642, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (550, b'User has no permission')

 

四、在工程中templates文件夹下新建mail文件夹,新建new_user.html,内容为

User <b>{{ user.username }}</b> has joined.

五、最后,上部分hello.py的代码

 

app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
app.config['FLASKY_MAIL_SUBJECT_PREFIX'] = '[Flasky]'
app.config['FLASKY_MAIL_SENDER'] ='Flasky Admin <xxx@163.com>'
app.config['FLASKY_ADMIN'] = os.environ.get('FLASKY_ADMIN')
def send_email(to, subject, template, **kwargs):
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX']+subject,
sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
    msg.body = render_template(template + '.txt', **kwargs)
    msg.html = render_template(template + '.html', **kwargs)
    mail.send(msg)

 

@app.route('/', methods=['GET', 'POST'])
def index():
    form = NameForm()
    if form.validate_on_submit():
        # old_name = session.get('name')
user = User.query.filter_by(username=form.name.data).first()
        if user is None:
            user = User(username=form.name.data)
            db.session.add(user)
            session['known'] = False
# if app.config['FLASKY_ADMIN']:
send_email(app.config['FLASKY_ADMIN'], 'New User', 'mail/new_user', user=user)
        else:
            session['known'] = True
session['name'] = form.name.data
        form.name.data = ''
# send_email(app.config['FLASKY_ADMIN'], 'New User')
        # send_email(app.config['FLASKY_ADMIN'], 'New User', 'mail/new_user', user=user)
return redirect(url_for('index'))
    return render_template('index.html', form=form, name=session.get('name'), known=session.get('known', False))

if __name__ == '__main__':
    app.run(debug=True)
    # manager.run()

运行,发送成功!Flask-mail发送邮件详细步骤及出现的错误
            
    
    博客分类: python3

  • Flask-mail发送邮件详细步骤及出现的错误
            
    
    博客分类: python3
  • 大小: 16.3 KB
  • Flask-mail发送邮件详细步骤及出现的错误
            
    
    博客分类: python3
  • 大小: 25.3 KB