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 .. :)
Related
I am working in a project where I need to send the email to the user who is filling the Email in the form. I am able to send email by using my Gmail Account details but while using outlook.365. This is the image with error that I am getting.
My requirement is :
Once users come in the registration form and fill the details as Name, Email, Mobile
Whatever Email is put there in the form, send the email with link to create new password.
Here is my code:
settings.py:
# Using outlook365:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'support#academic.com'
EMAIL_HOST = 'smtp.outlook.office365.com' # (also tried : smtp.office365.com and
outlook.office365.com)
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'My_Password'
# Using my gmail account:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'admin#gmail.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'My_Password'
views.py:
subject = "Academic - Create New Password"
message = "Hi %s! Please create your new Password here : http://127.0.0.1:8000/api/create-password" % firstname
send_mail(
subject, # Subject of the email
message, # Body or Message of the email
'support#academic.com', # from#gmail.com (admin#gmail.com for gmail account)
[email], # to#gmail.com # email that is filled in the form
)
Please guide me that what extra setting or configuration I need to send email using Outlook365.
Please put this in settings.py file.
SERVER_EMAIL = EMAIL_HOST_USER
Your settings look correct. Use EMAIL_HOST = "smtp.office365.com". Verify that you are using the correct credentials. Check that your outlook account is set up properly to send emails from support#academic.com.
I am trying sending email for password reset but it giving me following error.
SMTPSenderRefused at /account/password_reset/ (530, b'5.7.0 Must issue
a STARTTLS command first. x20sm7511321wme.6 - gsmtp',
'webmaster#localhost')......
here is my code.
EMAIL_USE_TSL = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'user#gmail.com'
EMAIL_PASSWORD = '*******'
EMAIL_PORT = 587
There's a typo. You wrote EMAIL_USE_TSL instead of EMAIL_USE_TLS.
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 found a lot of posts regarding send email with django, but I've a specific problem with gmail:
error image
here are my settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "myemail#gmail.com"
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = 'mypassword
EMAIL_PORT = 587
EMAIL_USE_TLS = True
and my view is:
from django.core.mail import EmailMessage, send_mail
if request.method == 'POST':
name = request.POST.get('name','')
email = request.POST.get('email','')
subject = request.POST.get('subject','')
text = request.POST.get('text','')
message = 'From: ' + email + '\n\n\n' + text
send_mail(subject, message, 'myemail#gmail.com', ['myemail#gmail.com'], fail_silently=False)
Can you please tell me why I get this error?
I also checked for google settings like "access from less secure applications" and similar
thank you in advance!!!
edit
I've already allowed less secure apps and clicked on unlock captcha
You can try this also.
import smtplib
def send_mail(request):
try:
subject = "Email Subject"
description ="description"
gmail_user = "from#gmail.com" # email id from where you want send mail
gmail_pwd ="password"
FROM = 'Admin: <from#gmail.com>'
TO = "to#gmail.com" #email id where you want send mail
TEXT = description
SUBJECT = subject
server = smtplib.SMTP_SSL()
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
message = """From: %s\nTo: %s\nSubject: %s\n\n%s """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
server.sendmail(FROM, TO, message)
server.quit()
except Exception,e:
print 'exception',e
hi please check add email id without the gmail extentio ie
EMAIL_HOST_USER = "myemail"
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/