Send email from gmail with Django and Python - python

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"

Related

Django didnt receive email

i am trying to send email from the website that i create. It does not display any error but there are no email that I received.
here is my setting code
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'my email's pswrd'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Here is the function
from django.core.mail import send_mail
def sentmes(request):
if request.method == "POST":
name = request.POST.get('name')
email = request.POST.get('email')
subject = request.POST.get('subject')
message = request.POST.get('message')
data = {
'name': name,
'email': email,
'subject': subject,
'message': message
}
mail = '''
New message: {}
From: {}
'''.format(data['message'], data['email'])
send_mail(data['subject'], mail, ' ', ['myemail#gmail.com'], fail_silently=False)
return render(request, ' ')
this is the output that i get
[14/Jun/2021 06:52:15] "POST / HTTP/1.1" 200 18516
Where is my mistake. Hope someone can help. Thank you.
You should have a closer look at the user's guide.
https://docs.djangoproject.com/fr/3.2/topics/email/
A good check is to use the console to have a feedback, while testing.
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
and to test your parameters via the shell.
If it works, switch to smtp,
It it works, then implement your function.
As you use the smtp backend, make sure that all the parameters are the right ones.

How to send Email in Django using office365

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.

Django does not send emails via smtp

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")

Django sending Email confusion

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 .. :)

Python Django Gmail SMTP setup

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/

Categories