0

I have a simple django application and that I can't seem to get to send emails. In my settings file I have:

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'MyHost@gmail.com'
EMAIL_HOST_PASSWORD = 'Mypassword'
DEFAULT_FROM_EMAIL = 'From Email'

The above values are just placeholders, I have the correct values in my settings.

The code for sending an email on model creation:

@receiver(post_save, sender = Alerta)
def my_callback(sender, created, **kwargs):
    if created:
        from_email = settings.DEFAULT_FROM_EMAIL
        print(from_email)
        try:
            send_mail(
                'Fogo Florestal',
                "Mensagem",
                from_email,
                ["some_destination@gmail.com"],
                fail_silently=False,
            )
        except Exception as e:
            print(e)

I also made sure that the Gmail account I am using can be used by less secure apps. Moreover when the email is not sent no exception is printed to the terminal

I have another web app which uses the the same send email procedure and it works properly.

Has anyone faced a similar problem? I am using python 3.5 and django 2.1

Opp
  • 520
  • 1
  • 8
  • 21
  • possible duplicate: https://stackoverflow.com/questions/6367014/how-to-send-email-via-django#6367458 – Michael D. Feb 16 '19 at 19:15
  • Is `DEFAULT_FROM_EMAIL` the same as `EMAIL_HOST_USER`? If you search stack overflow, you see many questions from users struggling to use gmail with Django, so you might find it easier to use a different provider e.g. mailgun or SES. – Alasdair Feb 16 '19 at 19:22
  • It is not the same – Augusto Peres Feb 16 '19 at 19:39

1 Answers1

0

Try to add this to settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Mahrez BenHamad
  • 1,791
  • 1
  • 15
  • 21