我有两个演示,第一个不起作用,但第二个有效。我想明白为什么。
错误:
raise err
error: [Errno 111] Connection refused
__init__.py
mail = Mail()
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_TLS = False
MAIL_USE_SSL= True
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
mail.init_app(app)
app.debug = True
return app
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
视图.py
from ..mymail import mmail
mmail();
我的邮件.py
from flask.ext.mail import Message
from . import mail
def mmail():
msg = Message(
'Hello',
sender='user@gmail.com',
recipients=
['xxxxxxxxxx@hotmail.com'])
msg.body = "This is the email body"
mail.send(msg)
return "Sent"
奇怪的是,这个例子可以正常工作:
from flask.ext.mail import Mail, Message
import os
DEBUG = True
MAIL_SERVER='smtp.gmail.com'
MAIL_PORT=465
MAIL_USE_TLS = False
MAIL_USE_SSL= True
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(__name__)
mail = Mail(app)
msg = Message(
'Hello',
sender='user@gmail.com',
recipients=
['xxxxxxxxxx@hotmail.com'])
msg.body = "This is the email body"
mail.send(msg)
return "Sent"
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
那么,问题是什么?