I am having troubles optimizing my python email sender function that uses flask-mail.
from flask_mail import Message, Mail
def send_email_msg():
with mail.connect() as emailConn:
with app.app_context():
for msg in msgList :
try:
emailConn.send(msg)
return 1
except smtplib.SMTPException, e:
return 0
I have tried using asynchronous calls to send the emails via threads but the solution was not very feasible for me as the error rate was relatively high. In addition I have removed the restriction for maximum number of emails that can be send per connection as well.
Currently, it takes about 1.4s to send a single email (with processing time about 1.6s). Ideally I would like to send a email within 0.6 to 0.8s.
Please advice me the possible libraries or solutions that I can use to achieve this. Since I am using amazon ses as my mail server, I have also tried out boto.ses with negligible performance difference. Please suggest to me the relevant libraries that I can use. Alternatively, are there any APIs which allow me to send multiple emails in one call (say I render 10 template emails but send them out at one go)?
Cheers.