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
Related
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'
I would like to send email from a django view. I got it to work on my local machine with django development server with the following settings:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'user#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
My view has
from django.core.mail import send_mail
def my_view(request):
send_mail('subject','body','user#gmail.com',['recipient#gmail.com'],fail_silently=False)
return render(request, 'index.html')
When I run send_mail() from manage.py shell on my production server, the email is successfully sent. However, when the view is invoked in production (nginx + uwsgi + django), no email is sent, even though the view runs without error and returns the expected response. I see no error in error.log.
Please help me set correct permissions and configurations for nginx so that this works.
NOTE: This question is similar to Send_mail in Django, works in shell, works locally, not in view, which was never resolved.
EDIT: When I do
sudo -u www-data python manage.py shell
to run as the nginx user, i can still successfully send mail, which confuses me even more.
If the email is printed in the console then you have probably set
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
which prints the email instead of sending it.
If this line is included in an alternative (to the default) settings file then it could somehow be used by your deployment setup, whereas the shell continues to use the default settings file where the backend is either set to:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
or not set at all, in which case the default value is smtp as well
I wrote a django app, and it was sending email fine. I am now porting it to use it with Google App Engine, but it won't send for some reason. What's going on?
settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xxxx#gmail.com'
EMAIL_HOST_PASSWORD = 'xxx'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
*views.py*
from google.appengine.api import mail
import webapp2
...
mail.send_mail(to="xxx#gmail.com", sender="test#test.com", subject="test", body="Hello World")
Nothing shows up! It's strange
You can't just use, nor specify any old smtp server with app engine. You have to use the provided mail api. The emails will also appear to come from the admin of the application.
The development web server can send email messages if configured to do
so with command-line options. It can use an SMTP server or the
Sendmail application, if available. When your application is running
on App Engine, it uses the App Engine mail service to send email
messages. See the Development Web Server for more information.
https://developers.google.com/appengine/docs/python/mail/sendingmail
Here's the example they give:
import webapp2
from google.appengine.api import mail
from google.appengine.api import users
class InviteFriendHandler(webapp2.RequestHandler):
def post(self):
user = users.get_current_user()
if user is None:
login_url = users.create_login_url(self.request.path)
self.redirect(login_url)
return
to_addr = self.request.get("friend_email")
if not mail.is_email_valid(to_addr):
# Return an error message...
pass
message = mail.EmailMessage()
message.sender = user.email()
message.to = to_addr
message.body = """
I've invited you to Example.com!
To accept this invitation, click the following link,
or copy and paste the URL into your browser's address
bar:
%s
""" % generate_invite_link(to_addr)
message.send()
Sending emails on Google App Engine is restricted, only email addresses which belongs to project owners or developers could be used.
and Django has it's own mechanism to send out email, for better integration, I will suggest you to use some Django library such as Django Rocket Engine.
download the source code and put it under you App Engine project, deploy it and put this line in settings.py:
EMAIL_BACKEND = 'rocket_engine.mail.EmailBackend'
after integrated that, you don't need to use GAE Email API by yourself, you just need to use the Django email API as usual, then emails got sent.
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()
Hay, i was wondering how i would go about setting up django so that i can send emails from a standard Leopard installation.
In php i just use mail() and it sends the mail for me.
Thanks
From the docs:
Mail is sent using the SMTP host and port specified in the EMAIL_HOST and EMAIL_PORT settings. The EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings, if set, are used to authenticate to the SMTP server, and the EMAIL_USE_TLS setting controls whether a secure connection is used.
So set your EMAIL_HOST to a friendly SMTP server which will relay mail for you, and away you go.
Again, from the docs:
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', 'from#example.com',
['to#example.com'], fail_silently=False)