当我尝试使用以下设置使用Flask-Mail向Gmail的SMTP服务器发送电子邮件时,我明白了[Errno -2] Name or service not known
.如何修复配置以使用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'
Run Code Online (Sandbox Code Playgroud) Flask Mail应用程序中使用的应用程序配置(遵循Miguel Grinberg Flask developlemt书籍):
app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
Run Code Online (Sandbox Code Playgroud)
邮件用户名和密码变量已正确设置并重新检查.在尝试使用以下代码发送消息时,
from flask.ext.mail import Message
from hello import mail
msg = Message('test subject', sender='same as MAIL_USERNAME', recipients=['check@mail.com'])
msg.body = 'text body'
msg.html = '<b>HTML</b> body'
with app.app_context():
mail.send(msg)
Run Code Online (Sandbox Code Playgroud)
发送时,应用程序会一次又一次地导致以下错误:
SMTPSenderRefused: (530, '5.5.1 Authentication Required. Learn more at\n5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 qb10sm6828974pbb.9 - gsmtp', u'configured MAIL_USERNAME')
Run Code Online (Sandbox Code Playgroud)
该错误的解决方法是什么?
当我尝试使用flask-mail通过我的Gmail帐户发送电子邮件时,我收到以下错误.
错误:[Errno 10061]无法建立连接,因为目标计算机主动拒绝它
我已经尝试过以各种方式配置flask-mail,但到目前为止我总是遇到这个错误.
以下是我尝试过的一些示例配置:
app = Flask(__name__)
mail = Mail(app)
app.config.update(dict(
DEBUG = True,
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 465,
MAIL_USE_TLS = False,
MAIL_USE_SSL = True,
MAIL_USERNAME = 'my_username@gmail.com',
MAIL_PASSWORD = 'my_password',
))
Run Code Online (Sandbox Code Playgroud)app = Flask(__name__)
mail = Mail(app)
app.config.update(dict(
DEBUG = True,
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 587,
MAIL_USE_TLS = True,
MAIL_USE_SSL = False,
MAIL_USERNAME = 'my_username@gmail.com',
MAIL_PASSWORD = 'my_password',
))
Run Code Online (Sandbox Code Playgroud)这个配置来自烧瓶mega-tutorial(http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xi-email-support)
app = Flask(__name__)
mail = Mail(app)
app.config.update(dict(
DEBUG = True,
# email server
MAIL_SERVER …
Run Code Online (Sandbox Code Playgroud)我发送带烧瓶邮件的电子邮件时遇到问题(http://pythonhosted.org/flask-mail/)
from flask.ext.sqlalchemy import SQLAlchemy
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from flask.ext.mail import Mail, Message
import os
# configuration
DEBUG = True
SECRET_KEY = 'hidden'
USERNAME = 'secret'
PASSWORD = 'secret'
MAIL_SERVER='smtp.gmail.com'
MAIL_PORT=587
MAIL_USE_TLS = False
MAIL_USE_SSL= True
MAIL_USERNAME = 'user@gmail.com'
MAIL_PASSWORD = 'password'
app = Flask(__name__)
mail = Mail(app)
@app.route('/minfo')
def send_mail():
msg = Message(
'Hello',
sender='user@gmail.com',
recipients=
['user@gmail.com.com'])
msg.body = "This is the email body"
mail.send(msg)
return "Sent"
Run Code Online (Sandbox Code Playgroud)
当我去/ …
我正在使用 Flask 邮件发送电子邮件。一切正常,除了我注意到发件人姓名只是电子邮件的第一部分,如 (1) - 在这种情况下为“信息”:
如何添加像 (2) 这样的自定义名称 - 看截图?
我发送电子邮件的代码:
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
def send_email(to, subject, template, **kwargs):
app = current_app._get_current_object()
msg = Message(app.config['MAIL_SUBJECT_PREFIX'] + ': ' + subject,
sender=app.config['MAIL_DEFAULT_SENDER'] , recipients=[to])
msg.body = render_template(template + '.txt', **kwargs)
msg.html = render_template(template + '.html', **kwargs)
thr = Thread(target=send_async_email, args=[app, msg])
thr.start()
return thr
Run Code Online (Sandbox Code Playgroud) 我在小型Web应用程序中使用Flask-Mail.由于应用程序很小,我使用gmail发送电子邮件.完成文档中的所有步骤后,运行应用程序以测试电子邮件功能.我继续得到一个error: [Errno 111] Connection refused
.
这是我在我的电子邮件设置config.py
:
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = 'some_user@gmail.com'
MAIL_PASSWORD = 'some_password'
DEFAULT_MAIL_SENDER = 'some_user@gmail.com'
Run Code Online (Sandbox Code Playgroud)
这是我用来测试我的Flask-Mail的视图views.py
:
@app.route('/', methods=['POST', 'GET'])
def index():
form = ContactForm()
if request.method == 'POST':
if form.validate():
msg = Message('New Msg - Test',
recipients=['some_user@gmail.com'])
msg.body = 'name=%s, email=%s \n Message:%s' % (form.name.data,
form.email.data,
form.message.data)
mail.send(msg)
flash('Message sent, I will contact you soon! Thanks')
return redirect(url_for('index'))
return render_template('index.html', form=form)
Run Code Online (Sandbox Code Playgroud)
我测试了是否有这样的防火墙问题:
In [2]: import …
Run Code Online (Sandbox Code Playgroud) 我正在使用Flask和Flask-Mail构建一个简单的联系页面.我按照本教程构建了应用程序 - 添加联系页面 - 现在当我尝试发送消息时,我收到了错误消息gaierror: [Errno -2] Name or service not known
.我一直在谷歌搜索错误一段时间,并没有在线找到任何类似的例子.我甚至无法弄清楚它找不到的名称或服务.
回溯页面将让我展开一行并执行一些Python代码.它提供了一个dump()
函数,它将向我显示所有变量,并且可以在对象上调用以查看其信息(如果这将有所帮助).
routes.py:
from forms import ContactForm
from flask.ext.mail import Message, Mail
mail = Mail()
app = Flask(__name__)
app.secret_key = 'development key'
app.config['MAIL_SERVER'] = 'smtp.google.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'email'
app.config['MAIL_PASSWORD'] = 'password'
mail.init_app(app)
@app.route('/contact', methods=['GET', 'POST'])
def contact():
form = ContactForm()
if request.method == 'POST':
if not form.validate():
Run Code Online (Sandbox Code Playgroud)
表格:
from flask.ext.wtf import Form, validators
from wtforms.fields import TextField, TextAreaField, SubmitField …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Huey 作为跨平台任务队列。我找到了https://github.com/pjcunningham/flask-huey-example,我已经克隆并设置了一个 virtualenv 来使用 conda (我正在 Windows 上工作)。我已按照更新的自述文件进行操作,并设法让所有三个窗口运行而不会出现错误。但是,当我打开http://localhost:6060/时
[![在此处输入图像描述][2]][2]
我单击发送按钮,这会破坏 Huey_consumer 进程:
$ python ...envs/hueytest1/Scripts/huey_consumer.exe run_huey.huey
[2018-08-06 10:19:25,949] INFO:huey.consumer:MainThread:Huey consumer started with 1 thread, PID 1704
[2018-08-06 10:19:25,949] INFO:huey.consumer:MainThread:Scheduler runs every 1 seconds.
[2018-08-06 10:19:25,949] INFO:huey.consumer:MainThread:Periodic tasks are enabled.
[2018-08-06 10:19:25,950] INFO:huey.consumer:MainThread:UTC is enabled.
[2018-08-06 10:19:25,950] INFO:huey.consumer:MainThread:The following commands are available:
+ send_async_email
+ dummy_task
[2018-08-06 10:19:39,743] INFO:huey.consumer.Worker:Worker-1:Executing queuecmd_send_async_email: ba5e092d-b1de-41cd-8b27-72d11c2b13d8
[2018-08-06 10:19:40,766] ERROR:huey.consumer.Worker:Worker-1:Unhandled exception in worker thread
Traceback (most recent call last):
File "...\envs\hueytest1\lib\site-packages\huey\consumer.py", line 153, …
Run Code Online (Sandbox Code Playgroud) 我正在为我的 Flask 应用程序使用 Flask-Mail 库,以便在用户注册添加到时事通讯时向他们发送默认的欢迎电子邮件。调试该库后,我发现它一次只能处理一个连接来发送消息,然后会自动关闭连接。如果后端将电子邮件发送到另一个用户同时连接仍处于打开状态则引发此异常:raise SMTPServerDisconnected("Connection unexpectedly closed: " smtplib.SMTPServerDisconnected: Connection unexpectedly closed: [WinError 10054] An existing connection was forcibly closed by the remote host
。我希望能够在连接关闭后将邮件 Mail 库排队以向另一个收件人发送新消息,但目前当我尝试将函数排队以发送消息时,它不断抛出我上面提到的错误。
工人.py:
import os
import redis
from rq import Worker, Queue, Connection
listen = ['high', 'default', 'low']
redis_url = os.environ.get('REDISTOGO_URL')
conn = redis.from_url(redis_url)
if __name__ == '__main__':
with Connection(conn):
worker = Worker(map(Queue, listen))
worker.work()
Run Code Online (Sandbox Code Playgroud)
用户路由.py
from flask import request, Blueprint, redirect, render_template
from flask_app import mail, db
from flask_app.users.forms import NewsLetterRegistrationForm
from …
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 Flask 邮件使用 gevent 在 Flask 中异步发送电子邮件。我正在“在应用程序上下文之外工作”。我知道 app.app_context() 但我无法让它与我的设置一起工作。
我的应用程序是用这样的应用程序工厂创建的:
我的项目/run_dev.py
from gevent.wsgi import WSGIServer
from my_project.app import create_app
from my_project.config import DevConfig
app = create_app(DevConfig)
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
Run Code Online (Sandbox Code Playgroud)
我的项目/我的项目/app.py
def create_app(config=None, app_name=None, blueprints=None):
app = Flask(app_name)
configure_app(app, config)
<other stuff>
return app
Run Code Online (Sandbox Code Playgroud)
以及我用来发送电子邮件的代码:
我的项目/我的项目/我的模块/views.py
@mymodule.route('/some/path/')
def do_something():
do_stuff(something)
Run Code Online (Sandbox Code Playgroud)
我的项目/我的项目/我的模块/utils.py
def do_stuff(something):
send_email(msg)
@async
def send_async_email(msg):
mail.send(msg)
def send_mail(request_id, recipients, email_type, env=None, pool=None):
msg = Message(
sender=sender,
recipients=recipients,
subject=subject,
body=body)
send_async_email(msg)
Run Code Online (Sandbox Code Playgroud)
我的项目/我的项目/decorators.py
def async(f):
def wrapper(*args, **kwargs): …
Run Code Online (Sandbox Code Playgroud) flask-mail ×10
flask ×9
python ×8
python-2.7 ×2
connection ×1
email ×1
gevent ×1
gmail ×1
python-huey ×1
smtp ×1
task-queue ×1
worker ×1