I am trying to send bulk emails with flask mail... here is my code
users = models.User.query.filter_by(query_email_notification=1).all()
if users:
# Bulk emails... keep connection open
with app.app_context():
with mail.connect() as conn:
for user in users:
subject = "subject"
message = "message"
msg = Message(recipients=[user.email],
body=message,
subject=subject,
sender='myemail@gmail.com')
conn.send(msg)
and my flask mail setup is as follows
from flask_mail import Message
app.config.update(dict(
DEBUG = True,
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 587,
MAIL_USE_TLS = True,
MAIL_USE_SSL = False,
MAIL_USERNAME = 'myemail@gmail.com',
MAIL_PASSWORD = 'password',
))
mail.init_app(app)
this works for a certain number of emails, but after about 100 emails I get
raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
I noticed that gmail limits the number of emails per day to 2000, but I made sure I do not hit that limit. any ideas what else I could check?
Is the MAIL_MAX_EMAILS config variable a solution? It will reconnect after a certain number of emails send...? I am looking for a reliable solution. So if sending many emails through gmail is not a good choice, what else would you recommend? thanks carl
Google drops the connection thinking it is a spam. Use a waiting time after each message sent successfully, something like:
time.sleep(80)
This will wait 80 seconds.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.