【问题标题】:flask email not sending烧瓶电子邮件未发送
【发布时间】:2016-02-04 07:57:51
【问题描述】:

我使用烧瓶,需要发送电子邮件

我的代码

from flask.ext.mail import Mail, Message
#mail config
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'mymail@gmail.com'
app.config['MAIL_PASSWORD'] = 'mypassword'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)

“发送”路由

@app.route('/send/')
def send():
    msg = Message('Hi', sender = 'mymail@gmail.com', recipients = ['recipient@gmail.com'])
    msg.body = "This is the email body sending with flask!"
    mail.send(msg)
    #msg.html = '<b>HTML</b> body'
    return "Sent"

但点击上述公司后,我得到了错误

TypeError: __init__() 接受 1 个位置参数,但给出了 2 个

File "I:\python\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "I:\python\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "I:\python\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "I:\python\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "I:\python\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "I:\python\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "I:\python\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "I:\python\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "I:\python\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "I:\python\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "G:\flask\fopster\hello.py", line 56, in send
msg = Message('Hi', sender = 'mymail@gmail.com', recipients = ['recipient@gmail.com'])

可能是什么问题?邮件地址的名称已更改。

【问题讨论】:

标签: python flask


【解决方案1】:

您正在实例化您的模型消息类,而不是烧瓶邮件消息类。

做一些类似的事情:

from flask.ext.mail import Mail, Message as MailMessage

@app.route('/send/')
def send():
    msg = MailMessage('Hi', sender = 'mymail@gmail.com', recipients = ['recipient@gmail.com'])
    msg.body = "This is the email body sending with flask!"
    mail.send(msg)
    #msg.html = '<b>HTML</b> body'
    return "Sent"

【讨论】:

  • 有人可以帮我澄清一下发件人和收件人吗?因为我设置了一个允许用户向我发送电子邮件的联系表单,我尝试了几组,它会向自己发送电子邮件,但最后我真的可以处理它,就像我有一封电子邮件并且我想接收电子邮件一样我该如何设置?
【解决方案2】:

尝试在导入后附加它

app=Flask(__name__)

您没有初始化应用程序。请通过此文档here 了解详细信息。

【讨论】:

  • from flask import Flask, redirect, render_template, session, url_for, flash from flask.ext.wtf import Form from flask.ext.sqlalchemy import SQLAlchemy from wtforms import StringField, SubmitField from wtforms.validators import Required from flask.ext.bootstrap import Bootstrap from flask.ext.script import Manager from flask.ext.script import Shell from flask.ext.migrate import Migrate, MigrateCommand from flask.ext.mail import Mail, Message import os app = Flask(__name__) 一切就绪
【解决方案3】:

检查一下。

from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)

mail=Mail(app)

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'your_email'
app.config['MAIL_PASSWORD'] = 'your_password'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_DEFAULT_SENDER'] = 'default_sender_email'
app.config['MAIL_ASCII_ATTACHMENTS'] = True
app.config['DEBUG'] = True

mail = Mail(app)

@app.route("/send")
def index():
    try:
        msg = Message('Subject', recipients = ['reciever_mail_id'])
        msg.body = "Hello Flask message sent from Flask-Mail"
        msg.html = "<b>TESTING HTML TAG</b>"
        mail.send(msg)
    except Exception as e:
        raise e
    return "Check Your Inbox !!!"

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2020-06-02
    相关资源
    最近更新 更多