'Django is not sending e-mails

guys!

I have an issue with my Django project.

About project:

Django version: 3.0.7
Django hosting provider: Digitalocean
E-mail hosting provider: Beget.com
OS: Ubuntu 18.04.6

Here is my settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.beget.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'MyVerySecretPassword'
DEFAULT_FROM_EMAIL='[email protected]'

Here is my views.py

from django.core.mail import send_mail

def register(request):
if request.method == 'POST':
    form = UserRegistrationForm(request.POST)
    if form.is_valid():
        user = form.save(commit=False)
        user.is_active = False
        user.save()
        current_site = get_current_site(request)
        mail_subject = 'Please, activate your account by clicking the link below.'
        message = render_to_string('acc_active_email.html', {
            'user': user,
            'domain': current_site.domain,
            'uid':urlsafe_base64_encode(force_bytes(user.pk)),
            'token':account_activation_token.make_token(user),
        })
        to_email = form.cleaned_data.get('email')

        send_mail(mail_subject, message, settings.DEFAULT_FROM_EMAIL, [to_email])

What I have tried to solve my problem:

  1. Using gmail account (yes, with anabled 'Allow less secure apps')
  2. Sending via Django shell (nope, it returns code '1', the mailbox is as empty as my ideas:( )
  3. I have opened 587 and 465 ports in ufw
  4. I tried to write and run simple python smtp script to send e-mail via Django shell (spoiler: it worked perfectly on my server via shell), but when I tried to implement this code into my Django code, it failed just like Django send_mail() function:

here is the code:

import smtplib

def send_email(user, pwd, recipient, subject, body):


    FROM = user
    TO = recipient if isinstance(recipient, list) else [recipient]
    SUBJECT = subject
    TEXT = body
    message = """From: %s\nTo: %s\nSubject: %s\n\n%s""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
    print(message)
    try:
        server_ssl = smtplib.SMTP_SSL("smtp.beget.com", 465)
        server_ssl.ehlo()
        server_ssl.login(user, pwd)
        server_ssl.sendmail(FROM, TO, message)
        server_ssl.close()
        print('successfully sent the mail')

    except:
        print("failed to send mail")

usn='[email protected]'
pwd='MyVerySecretPassword'
rc=['[email protected]']
subj='HoHoHo'
body='Hello, world'

send_email(usn,pwd,rc,subj,body)
  1. I checked all the variables (rendered them on the screen) which I tried to send via e-mail, all of them are correct.
  2. Sorry, I am not interested in third party projects like Sendgrid. I just want to understand what I am doing wrong.

Hope someone could help me to find the bug. Thank you very much!



Solution 1:[1]

I have found the solution. Thank to Solarissmoke for answering to my question.

The real thing was in fact that my hosting provider blocked outgoing traffic. So the best solution is to user third-party mail delivery provider like Google or Sendinblue. After I changed my settings, everything worked. Hope my post could help anyone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Senku