配置 Flask-Mail 以使用 GMail [英] Configure Flask-Mail to use GMail

查看:31
本文介绍了配置 Flask-Mail 以使用 GMail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用以下设置使用 Flask-Mail 向 Gmail 的 SMTP 服务器发送电子邮件时,我收到 [Errno -2] 名称或服务未知.如何修复我的配置以使用 Gmail 发送电子邮件?

When I try to send an email using Flask-Mail to Gmail's SMTP server using the settings below, I get [Errno -2] Name or service not known. How do I fix my configuration to send email with Gmail?

from flask import Flask, render_template, redirect, url_for
from flask_mail import Mail,  Message

app = Flask(__name__)
app.config.update(
    MAIL_SERVER='smtp@gmail.com',
    MAIL_PORT=587,
    MAIL_USE_SSL=True,
    MAIL_USERNAME = 'ri******a@gmail.com',
    MAIL_PASSWORD = 'Ma*****fe'
)

mail = Mail(app)

@app.route('/send-mail/')
def send_mail():
    msg = mail.send_message(
        'Send Mail tutorial!',
        sender='ri******a@gmail.com',
        recipients=['ri*********07@msn.com'],
        body="Congratulations you've succeeded!"
    )
    return 'Mail sent'

推荐答案

  1. 服务器是smtp.gmail.com".
  2. 端口必须与使用的安全类型相匹配.
    • 如果使用带有 MAIL_USE_TLS = True 的 STARTTLS,则使用 MAIL_PORT = 587.
    • 如果使用 MAIL_USE_SSL = True 直接使用 SSL/TLS,则使用 MAIL_PORT = 465.
    • 启用 STARTTLS 或 SSL/TLS,不要同时启用.
  1. The server is "smtp.gmail.com".
  2. The port must match the type of security used.
    • If using STARTTLS with MAIL_USE_TLS = True, then use MAIL_PORT = 587.
    • If using SSL/TLS directly with MAIL_USE_SSL = True, then use MAIL_PORT = 465.
    • Enable either STARTTLS or SSL/TLS, not both.

MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USERNAME = 'username@gmail.com'
MAIL_PASSWORD = 'app password generated in step 3'

这篇关于配置 Flask-Mail 以使用 GMail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆