How to Send an Email Django - django

Previously, I was using SendGrid to serve emails using Django's SMTP backend, which worked perfectly fine. However, now I would like my project to use Microsoft Exchange. When I updated my SMTP configuration in settings.py, upon the submission of some form to be emailed, the page timesout when trying to reach the server: TimeoutError: [Errno 60] Operation timed out.
settings.py
# E-Mail
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.microsoft365.com'
EMAIL_HOST_USER = 'username#domain.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
views.py
# Send email
send_mail('!!New Mail!! ', content, 'noreply#domain.com', ['username#domain.com'], fail_silently=False)

I solved my problem when I used the correct SMTP port (25, 465, 587) for my particular mail server.
settings.py
EMAIL_PORT = 25

Related

Django send_mail error using smtp.domain.com (smtplib.SMTPHeloError: (501, b'Syntactically invalid HELO argument(s)'))

I have a problem when sending an email using Django as the email I'm trying to send from is hosted on domain.com, I've tried to send from Gmail and it worked fine but when I use the configuration of domain.com it gives me this error: smtplib.SMTPHeloError: (501, b'Syntactically invalid HELO argument(s)')
what I understand is that it is related to the host name, but I don't know what to do to fix that error and can I use a different hostname for domain.com that works. below is the configuration in setting.py and send_mail function:
setting.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.domain.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'Name#emailFrom.com'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST_PASSWORD = "********"
send_mail function:
send_mail(
'HR Request',
'your request is being processed',
'Name#emailFrom.com',
['Name#emailTo.com'],
fail_silently=False,
)
return HttpResponse('Mail sent')

send_mail works locally but not on production hosting

The following works locally but as I deployed it to production hosting at Digital Ocean, the email is not sending as I test on shell command (python manage.py shell) like below. The send_mail line just got stuck there and am getting error: [Errno 101] Network is unreachable after few minutes. How can I capture the error on the email sending? Please advise how can I troubleshoot this issue.
from django.core.mail import send_mail
send_mail('test email', 'hello world', 'xxxx#gmail.com', ['xxxx#gmail.com'],fail_silently=False)
# Email settings
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'xxxx' #my gmail password
EMAIL_HOST_USER = 'xxxx#gmail.com' #my gmail username
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Your Django app settings look correct but you should also head over to https://accounts.google.com/DisplayUnlockCaptcha and make sure you enable access to lower security applications.
Also consider creating an application-specific password if you're using Two-Factor-Authentication.

SMTP connection timeout in Sentry

I'm using Sentry to monitor a Django app. I copied the following (correct and tested) email settings from the Django app to my Sentry config file:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_HOST_USER = 'info#ookmijnbedrijf.nl'
EMAIL_HOST_PASSWORD = '***'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_USE_TLS = False
SERVER_EMAIL = EMAIL_HOST_USER
Everything looks right on the SMTP Settings page, but when I try to send a test email I get this:
Connection unexpectedly closed: timed out
My Django app is sending emails correctly with these exact settings. What am I doing wrong?
Turns out Sentry only supports SMTP with TLS (not SSL). Made an issue about this.

How to setup Smtp and webfaction email host in a single settings.py file in django1.5.1?

This the one host that i created I want to add another one host also.
EMAIL_USE_TLS = True`
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587 `
EMAIL_HOST_PASSWORD = 'developer#sangeeth'
EMAIL_HOST_USER = 'developer.sangeeeth#gmail.com'
More than one EMAIL_HOST cannot be defined in django settings.py. Read more about email settings and sending emails in django. If you want to use different email hosts for sending emails, you can try python smtplib for sending emails instead of django's email feature.

Need clarification in Django mail sending scenario

I build a mail sending function in django.I did it by referring django Doc .
In doc to specify a email backend,need to add this line in settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
settings.py for email
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'anjunair#gmail.com'
EMAIL_HOST_PASSWORD = '*********'
DEFAULT_FROM_EMAIL = 'quartentine#name.com'
Mail is sending to the email if the EMAIL_BACKEND removed from settings.Need some clarification about this.
Thanks
The docs don't tell you to do that. That console email backend is specifically to NOT use SMTP - it outputs directly to the console.
Remove it, and keep it that way if you want to send via smtp (gmail).