Forums

Error with flask-mail (paid account, no google mail)

Hi, I'm having a problem with Flask-Mail and I am not able to send email from the deployed website. From the error.log this is the thrown error:

2020-07-29 00:01:59,997:   File "/usr/lib/python3.7/smtplib.py", line 359, in send
2020-07-29 00:01:59,997:     raise SMTPServerDisconnected('please run connect() first')
2020-07-29 00:01:59,997: smtplib.SMTPServerDisconnected: please run connect() first

In the server log there is no error.

And the weird thing is that the same code it correctly sends the email before deployment (from localhost) and, more weird, if I try to send a mail from a flask shell it works too. I searched online but I noticed that most of the answers were related to google problems, but my email is from another server (smtps.aruba.it). I also read that there could be a timeout problem for the spam, but the error happens while sending single emails.

Here is a sample of the code (but the problem shouldn't be the code since it is working from both localhost and flask shell).

Any help would be really really appreciated, because I really have no clues anymore :(

config.py

import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
    # Mail
    MAIL_SERVER = os.environ.get('MAIL_SERVER') #"smtps.aruba.it"
    MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25) #587
    MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None #1
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME') #"my@email.it"
    MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD') # HERE THE PASSWORD
    MAIL_DEFAULT_SENDER = os.environ.get('MAIL_DEFAULT_SENDER') #"my@email.it"
    ADMINS = ['my@email.it']

__init__.py

from flask import Flask
from config import Config
from flask_mail import Mail

app = Flask(__name__)
app.config.from_object(Config)
# add Email
email = Mail(app)

routes.py

@app.route('/', methods=['GET', 'POST'])
def navigation():
    form = FeedbackForm()
    try:
        if request.method == 'POST':
            feedbacksent = interface.take_care_of_the_feedback(form)
            return render_template(html_file)

interface.py

from app.forms import FeedbackForm
from app import mail
def take_care_of_the_feedback(form):
    if form.is_submitted():
        if form.validate_on_submit():
            mail.send_email_to_ourself(subject="[TEST] ",html_body="This is a test")
            return 1
        else:
            return -1

mail.py

from flask_mail import Message
from app import app,email

our_email = app.config["ADMINS"][0]

def send_email(subject, sender, recipients, text_body, html_body):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = text_body
    msg.html = html_body
    email.send(msg)

def send_email_to_ourself(subject,html_body):
    send_email(subject=subject, sender=our_email, recipients=[our_email], text_body="", html_body=html_body)

are you sending many email at once? and does it work initially then later fail? also when you checked this from the PythonAnywhere console, did you have to do anything (eg: did you have to setup app specific passwords or give an IP to your smtp provider?)

No, only one mail and also I'm sending the email form my account to my same account (it is just for me to keep trace of the feedback form).

No, it doesn't work neither at the beginning.

When testing it from the pythonanywhere console I just import the flask variables by using flask-shell. Then I just run:

from app import mail
mail.send_email_to_ourself("test","test")

and I correctly send and receive the email. The only email configuration that I set are the ones in the file config.py.

Could you give us the full stack trace of the error? It looks like you only posted the last few lines in your original message.

Here is the generated error.log

2020-07-29 00:01:59,994: [2020-07-29 00:01:59,992] ERROR in app: Exception on / [POST]
2020-07-29 00:01:59,994: Traceback (most recent call last):
2020-07-29 00:01:59,995:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 492, in send
2020-07-29 00:01:59,995:     message.send(connection)
2020-07-29 00:01:59,995:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 427, in send
2020-07-29 00:01:59,995:     connection.send(self)
2020-07-29 00:01:59,995:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 192, in send
2020-07-29 00:01:59,995:     message.rcpt_options)
2020-07-29 00:01:59,995:   File "/usr/lib/python3.7/smtplib.py", line 852, in sendmail
2020-07-29 00:01:59,995:     self.ehlo_or_helo_if_needed()
2020-07-29 00:01:59,995:   File "/usr/lib/python3.7/smtplib.py", line 600, in ehlo_or_helo_if_needed
2020-07-29 00:01:59,995:     if not (200 <= self.ehlo()[0] <= 299):
2020-07-29 00:01:59,995:   File "/usr/lib/python3.7/smtplib.py", line 440, in ehlo
2020-07-29 00:01:59,995:     self.putcmd(self.ehlo_msg, name or self.local_hostname)
2020-07-29 00:01:59,995:   File "/usr/lib/python3.7/smtplib.py", line 367, in putcmd
2020-07-29 00:01:59,996:     self.send(str)
2020-07-29 00:01:59,996:   File "/usr/lib/python3.7/smtplib.py", line 359, in send
2020-07-29 00:01:59,996:     raise SMTPServerDisconnected('please run connect() first')
2020-07-29 00:01:59,996: smtplib.SMTPServerDisconnected: please run connect() first
2020-07-29 00:01:59,996: 
2020-07-29 00:01:59,996: During handling of the above exception, another exception occurred:
2020-07-29 00:01:59,996: 
2020-07-29 00:01:59,996: Traceback (most recent call last):
2020-07-29 00:01:59,996:   File "/home/rafiki/app/routes.py", line 70, in navigation
2020-07-29 00:01:59,996:     feedbacksent = interface.take_care_of_the_feedback(form, feedback_folder)
2020-07-29 00:01:59,996:   File "/home/rafiki/app/src/interface.py", line 46, in take_care_of_the_feedback
2020-07-29 00:01:59,996:     all_good = write_feedback(form, feedback_folder)
2020-07-29 00:01:59,996:   File "/home/rafiki/app/src/interface.py", line 92, in write_feedback
2020-07-29 00:01:59,996:     mail.send_email_to_ourself(subject="[FEEDBACK] "+ category,html_body=mdfile)
2020-07-29 00:01:59,996:   File "/home/rafiki/app/mail.py", line 17, in send_email_to_ourself
2020-07-29 00:01:59,996:     send_email(subject=subject, sender=our_email, recipients=[our_email], text_body="", html_body=html_body)
2020-07-29 00:01:59,997:   File "/home/rafiki/app/mail.py", line 12, in send_email
2020-07-29 00:01:59,997:     email.send(msg)
2020-07-29 00:01:59,997:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 492, in send
2020-07-29 00:01:59,997:     message.send(connection)
2020-07-29 00:01:59,997:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 152, in __exit__
2020-07-29 00:01:59,997:     self.host.quit()
2020-07-29 00:01:59,997:   File "/usr/lib/python3.7/smtplib.py", line 984, in quit
2020-07-29 00:01:59,997:     res = self.docmd("quit")
2020-07-29 00:01:59,997:   File "/usr/lib/python3.7/smtplib.py", line 420, in docmd
2020-07-29 00:01:59,997:     self.putcmd(cmd, args)
2020-07-29 00:01:59,997:   File "/usr/lib/python3.7/smtplib.py", line 367, in putcmd
2020-07-29 00:01:59,997:     self.send(str)
2020-07-29 00:01:59,997:   File "/usr/lib/python3.7/smtplib.py", line 359, in send
2020-07-29 00:01:59,997:     raise SMTPServerDisconnected('please run connect() first')
2020-07-29 00:01:59,997: smtplib.SMTPServerDisconnected: please run connect() first
2020-07-29 00:01:59,997: 
2020-07-29 00:01:59,997: During handling of the above exception, another exception occurred:
2020-07-29 00:01:59,998: 
2020-07-29 00:01:59,998: Traceback (most recent call last):
2020-07-29 00:01:59,998:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 492, in send
2020-07-29 00:01:59,998:     message.send(connection)
2020-07-29 00:01:59,998:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 427, in send
2020-07-29 00:01:59,998:     connection.send(self)
2020-07-29 00:01:59,998:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 192, in send
2020-07-29 00:01:59,998:     message.rcpt_options)
2020-07-29 00:01:59,998:   File "/usr/lib/python3.7/smtplib.py", line 852, in sendmail
2020-07-29 00:01:59,998:     self.ehlo_or_helo_if_needed()
2020-07-29 00:01:59,998:   File "/usr/lib/python3.7/smtplib.py", line 600, in ehlo_or_helo_if_needed
2020-07-29 00:01:59,998:     if not (200 <= self.ehlo()[0] <= 299):
2020-07-29 00:01:59,998:   File "/usr/lib/python3.7/smtplib.py", line 440, in ehlo
2020-07-29 00:01:59,998:     self.putcmd(self.ehlo_msg, name or self.local_hostname)
2020-07-29 00:01:59,998:   File "/usr/lib/python3.7/smtplib.py", line 367, in putcmd
2020-07-29 00:01:59,998:     self.send(str)
2020-07-29 00:01:59,999:   File "/usr/lib/python3.7/smtplib.py", line 359, in send
2020-07-29 00:01:59,999:     raise SMTPServerDisconnected('please run connect() first')
2020-07-29 00:01:59,999: smtplib.SMTPServerDisconnected: please run connect() first
2020-07-29 00:01:59,999: 
2020-07-29 00:01:59,999: During handling of the above exception, another exception occurred:
2020-07-29 00:01:59,999: 
2020-07-29 00:01:59,999: Traceback (most recent call last):
2020-07-29 00:01:59,999:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
2020-07-29 00:01:59,999:     response = self.full_dispatch_request()
2020-07-29 00:01:59,999:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
2020-07-29 00:01:59,999:     rv = self.handle_user_exception(e)
2020-07-29 00:01:59,999:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
2020-07-29 00:01:59,999:     reraise(exc_type, exc_value, tb)
2020-07-29 00:01:59,999:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
2020-07-29 00:01:59,999:     raise value
2020-07-29 00:02:00,000:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
2020-07-29 00:02:00,000:     rv = self.dispatch_request()
2020-07-29 00:02:00,000:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
2020-07-29 00:02:00,000:     return self.view_functions[rule.endpoint](**req.view_args)
2020-07-29 00:02:00,000:   File "/home/rafiki/app/routes.py", line 82, in navigation
2020-07-29 00:02:00,000:     interface.take_care_of_the_error(request,e,error_folder)
2020-07-29 00:02:00,000:   File "/home/rafiki/app/src/interface.py", line 143, in take_care_of_the_error
2020-07-29 00:02:00,000:     mail.send_email_to_ourself(subject="[ERROR] "+ str(err),html_body=mdfile)
2020-07-29 00:02:00,000:   File "/home/rafiki/app/mail.py", line 17, in send_email_to_ourself
2020-07-29 00:02:00,000:     send_email(subject=subject, sender=our_email, recipients=[our_email], text_body="", html_body=html_body)
2020-07-29 00:02:00,000:   File "/home/rafiki/app/mail.py", line 12, in send_email
2020-07-29 00:02:00,000:     email.send(msg)
2020-07-29 00:02:00,000:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 492, in send
2020-07-29 00:02:00,001:     message.send(connection)
2020-07-29 00:02:00,001:   File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 152, in __exit__
2020-07-29 00:02:00,001:     self.host.quit()
2020-07-29 00:02:00,001:   File "/usr/lib/python3.7/smtplib.py", line 984, in quit
2020-07-29 00:02:00,001:     res = self.docmd("quit")
2020-07-29 00:02:00,001:   File "/usr/lib/python3.7/smtplib.py", line 420, in docmd
2020-07-29 00:02:00,001:     self.putcmd(cmd, args)
2020-07-29 00:02:00,001:   File "/usr/lib/python3.7/smtplib.py", line 367, in putcmd
2020-07-29 00:02:00,001:     self.send(str)
2020-07-29 00:02:00,001:   File "/usr/lib/python3.7/smtplib.py", line 359, in send
2020-07-29 00:02:00,001:     raise SMTPServerDisconnected('please run connect() first')
2020-07-29 00:02:00,002: smtplib.SMTPServerDisconnected: please run connect() first
2020-07-29 00:02:00,003: Exception on / [POST]
Traceback (most recent call last):
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 492, in send
    message.send(connection)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 427, in send
    connection.send(self)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 192, in send
    message.rcpt_options)
  File "/usr/lib/python3.7/smtplib.py", line 852, in sendmail
    self.ehlo_or_helo_if_needed()
  File "/usr/lib/python3.7/smtplib.py", line 600, in ehlo_or_helo_if_needed
    if not (200 <= self.ehlo()[0] <= 299):
  File "/usr/lib/python3.7/smtplib.py", line 440, in ehlo
    self.putcmd(self.ehlo_msg, name or self.local_hostname)
  File "/usr/lib/python3.7/smtplib.py", line 367, in putcmd
    self.send(str)
  File "/usr/lib/python3.7/smtplib.py", line 359, in send
    raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/home/rafiki/app/routes.py", line 70, in navigation
    feedbacksent = interface.take_care_of_the_feedback(form, feedback_folder)
  File "/home/rafiki/app/src/interface.py", line 46, in take_care_of_the_feedback
    all_good = write_feedback(form, feedback_folder)
  File "/home/rafiki/app/src/interface.py", line 92, in write_feedback
    mail.send_email_to_ourself(subject="[FEEDBACK] "+ category,html_body=mdfile)
  File "/home/rafiki/app/mail.py", line 17, in send_email_to_ourself
    send_email(subject=subject, sender=our_email, recipients=[our_email], text_body="", html_body=html_body)
  File "/home/rafiki/app/mail.py", line 12, in send_email
    email.send(msg)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 492, in send
    message.send(connection)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 152, in __exit__
    self.host.quit()
  File "/usr/lib/python3.7/smtplib.py", line 984, in quit
    res = self.docmd("quit")
  File "/usr/lib/python3.7/smtplib.py", line 420, in docmd
    self.putcmd(cmd, args)
  File "/usr/lib/python3.7/smtplib.py", line 367, in putcmd
    self.send(str)
  File "/usr/lib/python3.7/smtplib.py", line 359, in send
    raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 492, in send
    message.send(connection)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 427, in send
    connection.send(self)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 192, in send
    message.rcpt_options)
  File "/usr/lib/python3.7/smtplib.py", line 852, in sendmail
    self.ehlo_or_helo_if_needed()
  File "/usr/lib/python3.7/smtplib.py", line 600, in ehlo_or_helo_if_needed
    if not (200 <= self.ehlo()[0] <= 299):
  File "/usr/lib/python3.7/smtplib.py", line 440, in ehlo
    self.putcmd(self.ehlo_msg, name or self.local_hostname)
  File "/usr/lib/python3.7/smtplib.py", line 367, in putcmd
    self.send(str)
  File "/usr/lib/python3.7/smtplib.py", line 359, in send
    raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/rafiki/app/routes.py", line 82, in navigation
    interface.take_care_of_the_error(request,e,error_folder)
  File "/home/rafiki/app/src/interface.py", line 143, in take_care_of_the_error
    mail.send_email_to_ourself(subject="[ERROR] "+ str(err),html_body=mdfile)
  File "/home/rafiki/app/mail.py", line 17, in send_email_to_ourself
    send_email(subject=subject, sender=our_email, recipients=[our_email], text_body="", html_body=html_body)
  File "/home/rafiki/app/mail.py", line 12, in send_email
    email.send(msg)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 492, in send
    message.send(connection)
  File "/home/rafiki/.virtualenvs/myvirtualenv/lib/python3.7/site-packages/flask_mail.py", line 152, in __exit__
    self.host.quit()
  File "/usr/lib/python3.7/smtplib.py", line 984, in quit
    res = self.docmd("quit")
  File "/usr/lib/python3.7/smtplib.py", line 420, in docmd
    self.putcmd(cmd, args)
  File "/usr/lib/python3.7/smtplib.py", line 367, in putcmd
    self.send(str)
  File "/usr/lib/python3.7/smtplib.py", line 359, in send
    raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first

The error you're getting is coming from the SMTP server. Looks like you may be missing some config: https://stackoverflow.com/questions/24400368/smtpserverdisconnected-please-run-connect-first

@rafiki - it has been awhile since I had to fiddle with email debugging but I found an old line of code in my config that helped me. Try adding MAIL_DEBUG = 1 to your code.

It should log the SMTP conversation to the screen/logfile depending on your setup. See if that yields any clues.

Hello! We have been trying for a while, but we did not manage. The proposed solutions seems also not to work.

Our decision for now is to let it rest and try a different way, using the daily activity. We will try to send one mail per day (or per week) and see if this works. It could be easier, more regular and less confusing then triggering mail for a user action.

Thanks a lot to everyone who contributed, if we come to a final solution that works, we will share it.