Django EmailMessage not sending/timeout - django

im trying to use django.core.mail to send emails using the default backend and it doesn't seem to be working. I've set up the email credentials, server, and port number in the settings file but whenever I try to run the send() method of an email message the command hangs indefinitely.

views.py
from django.core.mail import send_mail
def sending_email(request):
message = ""
subject = ""
send_mail(subject, message, from_email, ['to_email',])
Add this in settings.py
# Sending mail
EMAIL_USE_TLS = True
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=587
EMAIL_HOST_USER='your gmail account'
EMAIL_HOST_PASSWORD='your gmail password'

I was having the same issue when trying to send via smtp.gmail.com with use_tls=True. It turns out I had the wrong port set. Here's what I'm doing now and it works:
from django.core.mail import get_connection
from django.core.mail.message import EmailMessage
connection = get_connection(use_tls=True, host='smtp.gmail.com', port=587,username='YourEmail#gmail.com', password='YourPassword')
EmailMessage('test', 'test', 'addr#from.com', ['addr#to.com'], connection=connection).send()

Related

How to send emails using RabbitMq and Django

I am new to RabbitMQ at the same time, if possible would someone also link me to a good tutorial for my task?
Here's a good tutorial for setup:
https://simpleisbetterthancomplex.com/tutorial/2017/08/20/how-to-use-celery-with-django.html#installing-rabbitmq-on-ubuntu-1604
More on RabbitMQ installation: https://www.rabbitmq.com/install-debian.html
After setting up you can send email creating a celery task.
ref: https://medium.com/#juwelariful1/send-mail-in-django-with-gmail-and-smtp-include-celery-and-gmail-configuration-4b07ae4f8542
More info on django docs:
https://docs.djangoproject.com/en/3.2/topics/email/
tasks.py
from celery import shared_task
from django.core.mail.message import EmailMultiAlternatives
#shared_task
def send_email_to(subject, body, from_email, to_email):
try:
email = EmailMultiAlternatives(subject, body, from_email, [to_email])
email.content_subtype = 'html'
email.send()
except:
pass

Django Sendgrid Heroku setup

I use a Django / Sendgrid / Heroku setup which was working well but it stopped with no apparent reason.
my django settings:
EMAIL_HOST="smtp.sendgrid.net"
EMAIL_HOST_PASSWORD="..."
EMAIL_HOST_USER="..."
EMAIL_PORT=587
EMAIL_USE_TLS=True
with this settings, if I send simple emails with the django library...
from django.core.mail import send_mail
send_mail(
'Subject here',
'Here is the message.',
from_email_address,
[to_email_address],
fail_silently=False,
)
I get this error
--> SMTPServerDisconnected: Connection unexpectedly closed
I can still send emails using the sendgrid python library
import sendgrid
sg = sendgrid.SendGridAPIClient(apikey)
sg.send(data)
Is there anything I am missing?
Just figured this out:
Sendgrid dynamically provides a password through the SENDGRID_PASSWORD Heroku environment variable. However the variable used for the email settings is EMAIL_HOST_PASSWORD which was not updated after our password changed.
Be sure that both variables are identical

Send all emails to specific email address instead for development in django

I would like to redirect all emails normally sent with send_mail() to me(and only me) when I work locally on the project.
I'm aware that I could use the file backend or console backend to see the emails but I need to be able to open the attached files so I can inspect them. Is there any way to do this easily?
Thanks!
Let's suposse you have a variable with the destination email.
destination_email = ...
In your settings add this:
# settings.py
DEBUG = True
...
DEBUG_DESTINATION_EMAIL = 'youremail#yourdomain.com'
So, where you send the email:
from django.conf import settings
...
if settings.DEBUG:
destination_email = settings.DEBUG_DESTINATION_EMAIL
else:
destination_email = ... # get the destination email normally
You could try writing your own custom email backend. You could subclass the smtp backend and change the recipients address before the email is sent.
Try django-email-bandit
Quoted from their readme.
To install django-email-bandit via pip:
pip install django-email-bandit
Add django-email-bandit to your installed apps:
INSTALLED_APPS = (
...
'bandit',
...
)
For your test environment you should enable the backend:
EMAIL_BACKEND = 'bandit.backends.smtp.HijackSMTPBackend'
and set the email which will receive all of the emails:
BANDIT_EMAIL = 'bandit#example.com'
I'm just writing simple email backend to send all emails to my debug email address.
import smtplib
from django.conf import settings
from django.core.mail.backends.smtp import EmailBackend
class TestEmailBackend(EmailBackend):
def _send(self, email_message):
message = email_message.message()
try:
self.connection.sendmail(settings.DEFAULT_FROM_EMAIL, settings.TEST_EMAIL_ADDRESS, message.as_bytes(linesep='\r\n'))
except smtplib.SMTPException:
if not self.fail_silently:
raise
return False
return True
You should set your email in TEST_EMAIL_ADDRESS variable in your settings.py and set EMAIL_BACKEND to 'path.to.your.module.TestEmailBackend'

Sending email from Django Mezzanine running on Heroku

I am using rackspace email and having problem sending email via Mezzanine(1.4.10) form page.
This is my settings:
EMAIL_HOST = 'secure.emailsrvr.com'
EMAIL_PORT = 465 # other ports also tried
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'info#example.com'
EMAIL_HOST_PASSWORD = 'secret'
This is Rackspace documentation:
http://www.rackspace.com/apps/support/portal/1088
But I can send email from the console like this:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from#example.com',
['to#example.com'], fail_silently=False)
It return 1 and I actually got the email. If I do that from Mezzanine, I assumed it also return 1 according the redirect URL after the email was sent, but I don't get the email.
In addition to the settings mentioned in the Django documentation, try setting DEFAULT_FROM_EMAIL. You will probably want to use the same value as EMAIL_HOST_USER.

django alternative EMAIL_HOST settings

I want to implement django managment command which send emails by smtp with not default settings from settings.py file such as:
EMAIL_HOST
EMAIL_HOST_USER
EMAIL_HOST_PASSWORD
FROM_MAIL
EMAIL_USE_TLS
i want to send with my alternative settings different from settings.py without change it email settings for all site.
how to implement this?
Define your alternate email settings and then create a new mail connection using those settings:
settings.py
ALTERNATE_EMAIL_HOST_PASSWORD = 'your password'
ALTERNATE_EMAIL_HOST_USER = 'your user'
ALTERNATE_EMAIL_HOST = ''
ALTERNATE_EMAIL_PORT = 123
ALTERNATE_EMAIL_USE_TLS = True
Then create new connection using those settings:
from django.core import mail
from django.core.mail import send_mail
from django.conf import settings
# create new connection
connection = mail.get_connection()
connection.password = settings.ALTERNATE_EMAIL_HOST_PASSWORD
connection.username = settings.ALTERNATE_EMAIL_HOST_USER
connection.host = settings.ALTERNATE_EMAIL_HOST
connection.port = settings.ALTERNATE_EMAIL_PORT
connection.use_tls = settings.ALTERNATE_EMAIL_USE_TLS
# send email using new connection you just created
send_mail('my subject', 'my message', settings.DEFAULT_FROM_EMAIL,
['abc#gmail.com'], connection=connection)