I am using the password reset function in django for resetting the password.
settings.py:
-------------
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xxxxxx#gmail.com'
EMAIL_HOST_PASSWORD = 'xxxxxxx#99'
EMAIL_PORT = 587
urls.py:
---------
url(r'^password_reset/$', auth_views.PasswordResetView, name='password_reset.html'),
views.py:
---------
def password_reset(request):
print ("entered the fn")
subject = "please change the password"
message = "please reset it"
to_list = ['xxxxxxxx#gmail.com']
send_mail(subject, message, to_list, fail_silently=True)
but when i am entering the email to reset it i am getting the following error:
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsX\n5.7.14 CBaTzAk4DIKFcdsgkGIv0Lgp1EdvehV4fsLoBw-Ix7_G5jQXYN8Ug0HFH-jO6UIjiar2nC\n5.7.14 Nd2dL4HXSYN4Oiazo88whyg8bSkbikpebbnb8E9JzDNTPT8s2b4vAgWrD87xNVpe1DGE94\n5.7.14 VGnf_nPjyyVW1R7xJaYpl8s23hB8fPcEYiPugPUPKjusMagyaOjZNG7v> Please log\n5.7.14 in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 d6sm10486629pfg.47 - gsmtp')
I suspect you are being stymied by GMail's account security features.
You need to likely follow these instructions:
https://support.google.com/mail/answer/7126229?visit_id=1-636656345878819046-1400238651&rd=1#cantsignin
This guide might also be of assistance: https://www.lifewire.com/get-a-password-to-access-gmail-by-pop-imap-2-1171882
Related
I am trying to send a mail from my django app via Amazon SES but I keep getting the following error:
smtplib.SMTPSenderRefused: (501, b'Invalid MAIL FROM address
provided', '=?utf-8?q?AKIAWMDVL5UEWNT3ODOO?=')
These are the settings that I am using:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'email-smtp.ap-south-1.amazonaws.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'smtp credential key'
EMAIL_HOST_PASSWORD = 'smtp credential password'
EMAIL_USE_TLS = True
and here's the code:
class CompanyCreateAPIView(APIView):
permission_classes = (permissions.AllowAny,)
def post(self, request):
email = request.data["company_email"]
phone = request.data["company_phone"]
def random_with_N_digits(n):
range_start = 10 ** (n - 1)
range_end = (10 ** n) - 1
return randint(range_start, range_end)
code = random_with_N_digits(4)
subject = 'Comapny Creation'
message = 'Company Code is {}'.format(code)
email_from = settings.EMAIL_HOST_USER
recipient_list = [str(email), ]
send_mail(subject, message, email_from, recipient_list)
I tried changing the port but it doesn't work, I have verified the email address in SES.. What is it that I am missing ?
You probably do not want to use the SMTP credentials username as the "email from" value. That should be something human readable, whatever you want your recipients to see as the address where the email came from. That address and/or domain also need to be whitelisted in the SES settings.
In Django's settings.py, set the DEFAULT_FROM_EMAIL setting to that address:
DEFAULT_FROM_EMAIL = 'info#example.com'
Do not pass email_from as an explicit setting to send_mail, unless you want to override it for specific emails (e.g. send from info#... for certain emails and newsletter#... for other kinds of emails).
I have the following in my settings.py:
EMAIL_HOST = 'mail.domain.com'
EMAIL_HOST_USER = 'user#domain.com'
EMAIL_HOST_PASSWORD = '******'
EMAIL_PORT = 567
EMAIL_USE_TLS = True
I have the following code to send email:
email = EmailMessage()
email.subject = subject
email.body = body
email.from_email = from_email
email.to = to
email.attach(file_name, pdf, 'application/pdf')
email.send()
Sometimes our server is down and there is no way for me to detect if the mail was sent or not. One way I can think of is to show Mail not sent error when Django cannot connect to mail server. How can I detect a failed connection to the server?
You can use try except.
try:
email.send()
except Exception as e:
print str(e.message)
# you can do some action.
I want to send an email from different email host depending on the status. Heres the pseudo-code
Currently using django send_mail and EmailMessage for the core.mail module
if status == "accepted":
letter = Letter().accept
# send from currentmail
msg = EmailMessage('blah blah', letter, 'currentmail', [to#mail.com])
else:
letter = Letter().decline
# send from other mail host
msg = EmailMessage('blah blah', letter, 'othermailhost', [to#mail.com])
msg.content_subtype = "html" # Main content is now text/html
msg.send()
and my settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'blah#gmail.com'
EMAIL_HOST_PASSWORD = '****'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
This isn't available in core.
If you want to use different smtp server based on your own rules then you need to create the connections manually, and python lets you do this by using smtplib:
https://docs.python.org/2/library/smtplib.html
registration app .
I follow toturial on http://agiliq.com/books/djenofdjango/chapter5.html for setting up an
email verfication and it suggest following confgiuration for setting.py
EMAIL_USE_TLS = True
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "user#example.com"
EMAIL_HOST_PASSWORD = "secret"
EMAIL_PORT = 587
but i got error about SMTP supporting of TLS in some forum I read to comment the EMAIL_USE_TLS
then I got the aut method error and again from some forum I read to comment EMAIL_HOST_USER = "user#example.com" and EMAIL_HOST_PASSWORD = "secret" but know I got the following error :
raise SMTPSenderRefused(code, resp, from_addr)
Exception Type: SMTPSenderRefused at /accounts/register/
Exception Value: (502, '5.5.1 Unrecognized command. h47sm103656655eey.13 - gsmtp', u'webmaster#localhost')
I'm really confused about how to config setting.py for sending email .
I was having the same problem. but I stumbled on this https://accounts.google.com/DisplayUnlockCaptcha and I followed. It tourned out the 2-Step-Verification wasn't allowing my app to send any mail.
I just clicked on the link related to 2-Step-Verification and end up Signing in using App Passwords
by changing :
EMAIL_HOST_PASSWORD = "secret"
to the generated 16-characters password :
EMAIL_HOST_PASSWORD = "newlygeneratedsecret"
Hope I helped. I'm told ( https://webapps.stackexchange.com/questions/44768/limitations-on-sending-email-through-gmail-smtp/48393#48393 ) that if Gmail sees you sending a lot of e-mails, it may request you to manually sign in. But I'm not sure.
Please try using the below code. I have used the below code and got the solution for the above-mentioned issue. I am sending HTML body that's why I have used MIMEText(variable, "html") if you want to use text please use MIMEText(variable, "text").
me = "your email"
you = "email of person whom you want to send the mail"
msg = MIMEMultipart("alternative")
msg["Subject"] = "Subject String"
msg["From"] = me
msg["To"] = you
# message to be sent
#for HTML as a part of mail body
html = '<html><body><p></a>Testing HTML/a></p></body></html>'
part2 = MIMEText(html, "html")
msg.attach(part2)
s = smtplib.SMTP("smtp.gmail.com", 587)
s.starttls()
s.login(me, "password of your mail")
# sending the mail
s.sendmail(me, you, msg.as_string())
# terminating the session`enter code here`
s.quit()
I have this in my setting.py file:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'from#gmail.com'
EMAIL_HOST_PASSWORD = 'Pass'
I want to send email to destinations posted from a template:
from django.core.mail.message import EmailMessage
destinations = request.POST['destinations'] #this return string with 2 emails ('fst#gmail.com; sd#gmail.com')
EmailMessage(subject, core, to=[destinations]).send()
it send email just to the first mail and not for others !
is there any action to make this work for all emails posted ?
Pass a list to to:
import re
# or you can use request.getlist('destination')
# I do not know how you generate the two mail addresses
destinations = re.split(r'[;\s]*', request.POST['destinations'])
EmailMessage(subject, content, to=destinations)