anymail / django-anymail

Django email backends and webhooks for Amazon SES, Brevo (Sendinblue), MailerSend, Mailgun, Mailjet, Postmark, Postal, Resend, SendGrid, SparkPost, Unisender Go and more
https://anymail.dev
BSD 3-Clause "New" or "Revised" License
1.67k stars 129 forks source link

OS Error when trying to send an email with anymail #293

Closed rkrishnasanka closed 1 year ago

rkrishnasanka commented 1 year ago

Reporting an error? It's helpful to know:

ANYMAIL = {
    "MAILGUN_API_KEY": "****************************",
    "MAILGUN_SENDER_DOMAIN": '**************************.mailgun.org',  # your Mailgun domain, if needed
}
EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"  # or sendgrid.EmailBackend, or...
DEFAULT_FROM_EMAIL = "you@example.com"  # if you don't already have this in settings
SERVER_EMAIL = "your-server@example.com"  # ditto (default from-email for Django errors)

send_mail("It works!", "This will get sent through Mailgun",
          "Anymail Sender <from@example.com>", ["r***************@gmail.com"])
2022-12-17 07:36:11,471 INFO [django.utils.autoreload:677] autoreload 2157 140443687850624 Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
December 17, 2022 - 07:36:14
Django version 4.1.4, using settings 
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
2022-12-17 07:36:27,925 INFO [django.utils.autoreload:266] autoreload 2157 140443687850624 /workspaces/upp/upp_bio/backend/upp_bio/settings.py changed, reloading.
Traceback (most recent call last):
  File "/workspaces/upp/upp_bio/backend/manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/.pyenv/versions/3.10.0/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/.pyenv/versions/3.10.0/lib/python3.10/site-packages/django/core/management/__init__.py", line 386, in execute
    settings.INSTALLED_APPS
  File "/.pyenv/versions/3.10.0/lib/python3.10/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/.pyenv/versions/3.10.0/lib/python3.10/site-packages/django/conf/__init__.py", line 79, in _setup
    self._wrapped = Settings(settings_module)
  File "/.pyenv/versions/3.10.0/lib/python3.10/site-packages/django/conf/__init__.py", line 190, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/.pyenv/versions/3.10.0/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/workspaces/upp/upp_bio/backend/upp_bio/settings_docker.py", line 1, in <module>
    from .settings import *
  File "/workspaces/upp/upp_bio/backend/upp_bio/settings.py", line 260, in <module>
    send_mail("It works!", "This will get sent through Mailgun",
  File "/.pyenv/versions/3.10.0/lib/python3.10/site-packages/django/core/mail/__init__.py", line 87, in send_mail
    return mail.send()
  File "/.pyenv/versions/3.10.0/lib/python3.10/site-packages/django/core/mail/message.py", line 298, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/.pyenv/versions/3.10.0/lib/python3.10/site-packages/django/core/mail/backends/smtp.py", line 124, in send_messages
    new_conn_created = self.open()
  File "/.pyenv/versions/3.10.0/lib/python3.10/site-packages/django/core/mail/backends/smtp.py", line 80, in open
    self.connection = self.connection_class(
  File "/.pyenv/versions/3.10.0/lib/python3.10/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/.pyenv/versions/3.10.0/lib/python3.10/smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/.pyenv/versions/3.10.0/lib/python3.10/smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/.pyenv/versions/3.10.0/lib/python3.10/socket.py", line 845, in create_connection
    raise err
  File "/.pyenv/versions/3.10.0/lib/python3.10/socket.py", line 833, in create_connection
    sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
medmunds commented 1 year ago

@rkrishnasanka it looks like you're trying to call Django's send_mail() function directly from your settings.py file. That won't work—the Django settings file should contain only settings, never calls to to Django framework functions.

If you're just trying to test that send_mail() works with your Anymail settings, move the call to some module where Django framework code is allowed and that is always loaded—like apps.py or urls.py. Or you could try Django's built-in interactive shell.

(Calling send_mail() from settings.py ends up trying to use Django's default SMTP email backend, rather than Anymail, because Django has not finished loading its settings at that point. The error you're seeing is because your system is not configured for sending mail through SMTP.)

rkrishnasanka commented 1 year ago

Ah I see, I in that case I'm guessing that it should be flagged as an issue on Django for not making this a better handling of the error / print out a better error message.

Thanks for the help !