Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smtplib issues when using celery to send mails async #17

Open
Lunarequest opened this issue Apr 11, 2021 · 2 comments
Open

smtplib issues when using celery to send mails async #17

Lunarequest opened this issue Apr 11, 2021 · 2 comments

Comments

@Lunarequest
Copy link

I have been trying to send a user activation mail with flask-mailman and come across this error. I don't quite understand why it happens. have I misconfigured something?
the function that causes this error

@celery.task
def send_email(username, email, url):
    app = create_app()
    with app.app_context():
        with mail.get_connection(backend="smtp") as connection:
            msg = EmailMessage(
                "Activate Your account",
                f"{username} please confirm your email by going to {url}",
                app.config["MAIL_USER"],
                [email],
                connection=connection,
            )
            msg.send(fail_silently=False)
2021-04-11 18:05:21,818: ERROR/ForkPoolWorker-4] Task argboard.routes.auth.auth.send_email[8c6ab451-ba1a-4780-80aa-76916670c8ec] raised unexpected: SMTPServerDisconnected('please run connect() first')
Traceback (most recent call last):
  File "/home/nullrequest/.local/share/virtualenvs/argboard-Q0PubHYB/lib/python3.9/site-packages/celery/app/trace.py", line 405, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/home/nullrequest/.local/share/virtualenvs/argboard-Q0PubHYB/lib/python3.9/site-packages/celery/app/trace.py", line 697, in __protected_call__
    return self.run(*args, **kwargs)
  File "/home/nullrequest/argboard/argboard/routes/auth/auth.py", line 28, in send_email
    with mail.get_connection(backend="smtp") as connection:
  File "/home/nullrequest/.local/share/virtualenvs/argboard-Q0PubHYB/lib/python3.9/site-packages/flask_mailman/backends/base.py", line 50, in __enter__
    self.open()
  File "/home/nullrequest/.local/share/virtualenvs/argboard-Q0PubHYB/lib/python3.9/site-packages/flask_mailman/backends/smtp.py", line 67, in open
    self.connection.starttls(keyfile=self.ssl_keyfile, certfile=self.ssl_certfile)
  File "/usr/lib/python3.9/smtplib.py", line 753, in starttls
    self.ehlo_or_helo_if_needed()
  File "/usr/lib/python3.9/smtplib.py", line 604, in ehlo_or_helo_if_needed
    if not (200 <= self.ehlo()[0] <= 299):
  File "/usr/lib/python3.9/smtplib.py", line 444, in ehlo
    self.putcmd(self.ehlo_msg, name or self.local_hostname)
  File "/usr/lib/python3.9/smtplib.py", line 371, in putcmd
    self.send(str)
  File "/usr/lib/python3.9/smtplib.py", line 363, in send
    raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first
@waynerv
Copy link
Owner

waynerv commented Apr 26, 2021

Sorry I've never used celery, so I'll need some time to investigate the problem you describe.

@jwag956
Copy link

jwag956 commented Apr 4, 2022

FWIW - I can run with Celery with no problems. One possible thing I see is that you are doing the create_app() inside the celery task - not sure why you would do that - presumably, you are using Flask and should have already created the 'app' and use that SAME global app to set up a context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants