Failed to send Email by Django

58 views
Skip to first unread message

bing

unread,
Mar 24, 2023, 8:34:47 AM3/24/23
to Django users
I am using the email module in Django to inform of users with their registration. Sometimes it can send email successfully, but more often, it fails. I searched for many solutions, but did not change this situation.

The following is what I have configured:
settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.139.com'
EMAIL_PORT = 25
EMAIL_HOST_USER = 'EmailAddress'
EMAIL_HOST_PASSWORD = 'xxxxxxxxxxxxxxxx'
EMAIL_FROM = 'xxxxxx<EmailAddress>'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_USE_TLS = False  


views.py:
import smtplib
def sendEmail(subject, message, from_email, to_email):
    try:
        send_mail(subject, message, from_email, [to_email], fail_silently=False)
        return True
    except smtplib.SMTPException as e:
        print(e)
        return False

Although I used the 'try', 'except' to catch the error, I still find no results in the output. Is there anything wrong? I would be very grateful if someone can give me some advice. Thanks in advance.

Jd Mehra

unread,
Mar 24, 2023, 8:37:37 AM3/24/23
to Django users
Are you sure your email_host in settings.py is reachable?

Kasper Laudrup

unread,
Mar 24, 2023, 9:34:03 AM3/24/23
to django...@googlegroups.com
On 24/03/2023 05.16, bing wrote:
> I am using the email module in Django to inform of users with their
> registration. Sometimes it can send email successfully, but more often,
> it fails. I searched for many solutions, but did not change this situation.
>

That is not really related to Django but more related to your email
provider (or SMTP server).

It seems like you're using some Chinese email provider (139.com) and I
have no idea what their reputation is, but it is completely up to the
receiver of the email (or their email provider) whether they want to
accept the email or not. If they don't it will most often just fail
silently as you experience.

Not much to do about that. That's just how SMTP (email) has been
designed (or not) back in the days.

You could try using another provider or setting up your own SMTP server
(which is far from trivial to do and maintain).

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages