Yax
Yax

Reputation: 2189

Django Not Sending Email

I am trying to send a mail using Django's send_mail() from my local machine but having some challenges.

I have the email settings set in my settings.py thusly:

settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = '1025'
EMAIL_USE_TLS = True
LANGUAGE_CODE = 'en-us'

views.py:

def send_greetings(request):
    from django.core.mail import send_mail
    send_mail('Invitation', 'Hi, how are you doing today?', '[email protected]', [[email protected]], fail_silently=False)

If I try the code above, I get:

SMTPException at /mysite/
STARTTLS extension not supported by server.

If comment #EMAIL_USE_TLS = True in settings.py file. It doesn't show any error but I don't receive any email in my Gmail account.

If I change the EMAIL_PORT = 25 I get:

error at /mysite/
[Errno 10061] No connection could be made because the target machine actively refused it

Upvotes: 2

Views: 1047

Answers (2)

abhinav
abhinav

Reputation: 687

Can you try the below. send_mail returns a value which you are not returning in your method.

def send_greetings(request):
    from django.core.mail import send_mail
    result = send_mail('Invitation', 'Hi, how are you doing today?', '[email protected]', [[email protected]], fail_silently=False)
    return result

Upvotes: 0

Sibtain
Sibtain

Reputation: 1649

For me this worked perfectly, try this:

settings.py

# Change HOST, HOST_USER, and HOST_PASSWORD
EMAIL_USE_TLS=True
EMAIL_HOST='mail.example.com'
EMAIL_HOST_USER='[email protected]'
EMAIL_HOST_PASSWORD='test123'
EMAIL_PORT=26

views.py

def send_greetings(request):
     ...
     subject = 'email subject'
     message = "Hello"
     from_email = settings.EMAIL_HOST_USER
     to_email = "[email protected]"
     recipient = [to_email]
     send_mail(subject, message, from_email, recipient, fail_silently=True)
     ...

Upvotes: 1

Related Questions