I am trying to send email from Django by setting up gmail smtp.
But everytime it is returning me 0 status. I have searched different relevant answers in stackoverflow and i am setting up the smtp server the same way but still it is not sending any email..
Below is my setting file
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'my gmail account'
EMAIL_HOST_PASSWORD = 'my gmail account password'
DEFAULT_FROM_EMAIL = 'my gmail account'
DEFAULT_TO_EMAIL = 'to email'
Below is my code
from django.conf import settings
from django.core.mail import send_mail
print "Sending Email"
mail_title = 'Test Email'
message = 'This is a test email.'
email = settings.DEFAULT_FROM_EMAIL
recipients = [settings.DEFAULT_TO_EMAIL]
print send_mail(mail_title, message, email, recipients, settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD)
print "Email Sent"
But everytime it print status 0 which means email is not sent. About the environment i am running this code on Amazon EC2 instance which has ubuntu as an OS and Apache as server..
Do i need to do additional setups for sending email through gmail smtp??
Much appreciate your help
Thanks in advance
Your gmail.smtp setup is correct. It looks like you are not calling the send_email function correctly, and that's why it's not sending. In the python shell, try the following:
import django
from django.conf import settings
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', settings.EMAIL_HOST_USER,
['to#example.com'], fail_silently=False)
Try to change EMAIL_USE_TLS=True to EMAIL_USE_SSL=True and EMAIL_PORT=465
https://docs.djangoproject.com/en/1.10/topics/email/
Related
I am trying to send a password reset email to users, via an App we are developping with Django.
When trying the App locally, the user can select to reset a pwd if forgotten , input his email into a field and submit, in order to receive an email. The sender email is a business email address.
Checking into Sendgrid, I can see the activity log that the email has been processed and delivered. So it seems working.
However, when trying to do the same passing via Github, Azure, on https://XXXXXX.azurewebsites.net/en/password_reset/, I get the following :
SMTPSenderRefused at /en/password_reset/
(550, b'Unauthenticated senders not allowed', 'nicolas#XXXXX.com')
in the log I get the following as well:
raise SMTPSenderRefused(code, resp, from_addr)
Is there something I am missing with Azure, another key to include. I read through many similar issues, but could not find a suitable response to my problem. The fact Sendgrid works locally but not with Azure make me think I am missing a connection at that level. Otherwise, All other aspects of the App works when hosting it on Azure..
Below are the codes I am using:
in settings.py
import os
ALLOWED_HOSTS = ['XXXXXX.azurewebsites.net']
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' #Exactly that
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'nicolas#XXXXX.com'
in views :
from django.core import mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
def send_password_reset_email(request):
subject = 'email reset'
html_message = render_to_string('password_reset_email.html', {'context': 'values'})
plain_message = strip_tags(html_message)
from_email = 'nicolas#XXXXX.com'
form = (request.POST)
if form.is_valid():
data = form.cleaned_data
to=data.get("email")
mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message,fail_silently=False)
in url.py:
urlpatterns=[
url('', views.send_password_reset_email)
]
merci
Nicolas
This is solved: If SendGrid works locally, but not on Azure:
In Azure, select ‘configuration', 'Application settings', 'new application settings' and then, as per below, using the correct API name made it work.
In SendGrid, I had named my API key ‘test’, and thought in Azure I should name it the same. However, I should have named it the same as in my code:
SENDGRID_API_KEY
Not obvious at first.
I'm trying to send a email via smtp using Django. When I try to send the email, I do not get any error, but my application does not respond and it keeps waiting on to send the email, but obviously it does not send nothing.
I have tried with smtp gmail and smtp hotmail, but it is not working. I have already checked my Windows firewall, and again it is not working. I have tried to send the email using Python shell but it does not send nothing.
I think that I have tried almost everything that I saw on the other posts here in Stack Overflow.
settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.office365.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail#hotmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
SERVER_EMAIL = EMAIL_HOST_USER
mails.py:
from django.conf import settings
from django.template.loader import get_template
from django.core.mail import EmailMultiAlternatives
class Mail:
#staticmethod
def send_complete_order(orden, user):
subject = 'order sent'
template = get_template('orders/mails/complete.html')
content = template.render({
'user': user
})
message = EmailMultiAlternatives(subject, 'Testing',
settings.EMAIL_HOST_USER, [user.email])
message.attach_alternative(content, 'text/html')
message.send()
views.py:
#login_required(login_url='login')
def complete(request):
cart = get_or_create_cart(request)
order = get_or_create_order(cart, request)
if request.user.id != order.user_id:
return redirect('carts:cart')
order.complete()
Mail.send_complete_order(order, request.user)
destroy_cart(request)
destroy_order(request)
messages.success(request, "order complete")
return redirect('index')
Create a secure SSL context
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL(smtp_mail, port, context=context) as server2:
server2.login(your_mail, passwort)
server2.sendmail(mail_from, mail_to, message.as_string())
print("Successfully sent email")
Difficult to phrase this question and what I've done as I don't know where the error is occuring.
I'm using Django hosted on AWS Elastic Beanstalk and SES to send emails. (Only for password reset) However the password reset emails don't seem to be sending.
When I try to send an email as below however it works.
send_mail('Test', 'Email content', 'email#email.com',['email#email.com',])
Also locally the password reset email sends (its put in spam but that's a different issue)
My email settings are:
EMAIL_HOST = 'smtp.gmail.com' # mail service smtp
EMAIL_HOST_USER = 'email#email.com' # email id
EMAIL_HOST_PASSWORD = '****' #password
EMAIL_PORT = 587
# EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = 'ACCESSKEY'
AWS_SECRET_ACCESS_KEY = 'SECRETKEY'
AWS_SES_REGION_NAME = 'eu-west-1'
AWS_SES_REGION_ENDPOINT = 'email.eu-west-1.amazonaws.com'
DEFAULT_FROM_EMAIL= 'email#email.com'
If it's relevant, anywhere I've written email#email.com is actually the same email.
Any help would be much appreciated.
Thanks
I've used one application with sending email to the user for notification. but in that application setting.py file contains some confusing terms like
in
setting file
EMAIL_USE_TLS = True
I'm not sure what this is and also
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'rquest186#gmail.com'
what is the variety in this two
and if host is declared here then
mail.py
def send_mail(title,message,reciver):
try:
mailS = 'smtp.gmail.com'
mailP = 587
mailUsr = "idefusiontest2015#gmail.com"
mailPass = "********"
# Create a text/plain message
msg = MIMEMultipart('alternative')
msg['Subject'] = title
msg['From'] = mailUsr
msg['To'] = reciver
html="<html><head></head><body><p>"+message.replace("\n","<br>")+"</p></body></html>"
part2 = MIMEText(html, 'html')
msg.attach(part2)
# Send the message via our own SMTP server, but don't include the
s = smtplib.SMTP(mailS,mailP)
s.ehlo()
s.starttls()
s.ehlo()
s.login(mailUsr,mailPass)
s.sendmail(mailUsr, [reciver], msg.as_string())
s.quit()
except Exception as e:
print(e)
in this
mailUsr = "idefusiontest2015#gmail.com" ???? what's this for ?
I'm new to this. and it's confusing for me.
thanks in adv.
You need to provide a gmail account to send mail from:
mailUsr = "idefusiontest2015#gmail.com"
mailPass = "IDEF2017"
MailUsr is the emailid and MailPass is passowrd of that account.
It is like for sending mail you are logging to gmail account using these emailid and password and then sending the mail.
So if you want to send then you need to use your emailid and password in place of that.
settings.py
You need to have these settings.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'username'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST is the smtp service you are using. In my case it is sendgrid in your case gmail.
EMAIL_HOST_USER is the username from that smtp service in your case your gmailid and EMAIL_HOST_PASSWORD password for that account.
I think it should be clear to you by now.
The the values from settings.py file will be automatically used while sending mail when if you are using django mail sending package like EmailMessage() for SendMail link
the second one that you are using is native python package . on that you have to explicitly provide the username and password and other configurations
sorry about the language .. :)
I am trying to build a online book store and I am trying to send an verification email to new signed users, but for some reason Email is not getting sent, I also tried to use app specific password in gmail.
A help will be greatly appreciated.
Here is my Email code in settings.py
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST='smtp.gmail.com'
EMAIL_HOST_USER='kshitu.rangari#gmail.com'
EMAIL_HOST_PASSWORD='jgpcjiajvklxraof'
EMAIL_PORT=587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'books#pickabook.com'
Code in my Views.py
from .models import Book
def index (request):
return render (request,'template.html')
def store (request):
return render (request, 'store.html' )
def store (request):
count = Book.objects.all().count()
context = {
'count':count,
}
return render (request,'store.html',context)
As you already have the settings for email ready and to test whether emails are being sent, do the following
In settings.py make
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Run interactive mode,
python manage.py shell
Import the EmailMessage module,
from django.core.mail import EmailMessage
Send the email,
email = EmailMessage('Subject', 'Body', to=['kshitu.rangari#gmail.com'])
email.send()
If the above works successfully then your gmail account is configured to send emails.
For Gmail specifically, you might need to toggle the "enable less secure apps" function within your Google account.
https://support.google.com/accounts/answer/6010255?hl=en