I set my gmail account to allow "Less secure apps access" and I use server.starttls() in smtplib in python to make sure the session is encrypted so why is google still periodically rejecting my login with "Please log in via your web browser and then try again. Learn more at https://support.google.com/mail/answer/78754 "
server = smtplib.SMTP(GMAIL_SERVER, GMAIL_PORT)
server.ehlo()
server.starttls()
server.login(GMAIL_USER, GMAIL_PASS)
server.sendmail(GMAIL_USER, [to_address], mime_msg.as_string())
server.close()
logger.info("Email successfully sent")
When using the Gmail SMTP from a programming a lot of times you will get an error with it rejecting your login. It wants you to use the browser using Xoauth2.
A alternative would be to try and use an apps password infested of your true password. In my experience this tends to fix the issue, its often the case when the user has 2fa enabled.
Related
This is my code for the gmail smtp server.
def verify_email(self, user_email, sending_email, password):
port = 465
self.sending_mail = sending_email
self.password = password
if (re.fullmatch(regex, user_email)):
self.user_email = user_email
else:
raise Exception("Sorry, your email is invalid.")
self.email_msg = f"""
Subject: Verify Email
Is this you, {self.username}?
Please enter the code below to verify:
[{verify_code}]
From,
{self.sending_mail}
"""
context = ssl.create_default_context()
try:
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login(self.sending_mail, self.password)
server.sendmail(self.sending_mail, self.user_email, self.email_msg)
except:
print("An error has happened. Please check the spelling of your email, "
"or try again. 🙂")
How can I use the 10 minute mail SMTP server?
I used Google but I didn't understand any of it.
When I used Gmail I got this error:
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials g15-20020a056a0023cf00b004e17e11cb17sm9404507pfc.111 - gsmtp')
So I decided to use 10minute mail. In my gmail account, I can't change the settings to allow Less Secure Apps.
Gmail recently announced that they wouldn't allow unsecure connections like SMTP on May 30th of 2022.
Using SMTP through gmail requires that you enable the setting "Allow less secure apps" in account settings to be able to connect.
I'd try something like Yahoo, or Outlook, or another email provider that allows SMTP. Look up their server information, and what security privilege's you need to enable for the connection to happen for which provider you choose.
I'm not familiar with 10 Minute Mail, but just wanted to give that insight to Gmail's SMTP server. Hopefully someone else can help you set it up through 10 minute mail.
I am currently trying to send emails via smtplib with the following code:
import smtplib
import email.utils
from email.mime.text import MIMEText
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login('username#gmail.com', 'pwd')
msg['Subject'] = "subject line"
msg['From'] = 'newusername#gmail.com'
msg['To'] = 'friend#gmail.com'
smtpserver.sendmail(sender, recipients, msg.as_string())
When I do something like this, instead of the recipient getting an email from newusername#gmail.com, they get it from username#gmail.com, which was the email I used for authentication.
Is there a way to change this?
This is an intentional security feature in gmail, and other public mail servers, called SMTP AUTH.
Without this feature, anyone with a gmail address could send mail impersonating anyone else with a gmail address. I could send a message claiming to be from you, and the recipient would have no way of knowing it wasn't from you, and you'd have no way to prove it wasn't from you.
But it wouldn't matter anyway, because spammers would be sending so much more email with your address than you do that email addresses would be effectively meaningless, and the whole email system would fall apart. Which is what almost happened in the late 90s. Only a concerted campaign to require SMTP AUTH on all open submission servers, including blacklisting all mail from servers that didn't comply (even the ones that used POP-before-SMTP, IMAP-before-SMTP, IP/MAC verification, or other alternatives to SMTP AUTH) managed to keep the spammers from ruining everything.
Later, another security/anti-spam measure, called DKIM, was added, which gmail also uses: most servers will throw out any messages that isn't signed by the originating server, indicating that the server trusts that the message came from who it says it came from. Obviously, gmail isn't going to certify that a message came from newusername when it actually came from username.1 And, if they did, people who administer other servers would just blacklist gmail signatures are meaningless.
1. Unless they have some reason to trust that username has the right to send mail as newusername—corporate mail servers sometimes do have a feature like that, allowing you to configure things so, e.g., a secretary can send mail from his boss's address without having his boss's password.
This question already has answers here:
How to send an email with Gmail as provider using Python?
(16 answers)
SMTPAuthenticationError 5.7.14 Please log\n5.7.14 in via your web browser
(10 answers)
Closed 7 years ago.
when i try to send mail using gmail and python error occurred this type of question are already in this site but doesn't help to me
gmail_user = "me#gmail.com"
gmail_pwd = "password"
TO = 'friend#gmail.com'
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_user,
'Subject: %s' % SUBJECT,
'', TEXT])
server.sendmail(gmail_user, [TO], BODY)
print ('email sent')
error:
server.login(gmail_user, gmail_pwd)
File "/usr/lib/python3.4/smtplib.py", line 639, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14
<https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtl1\n5.7.14 Li2yir27TqbRfvc02CzPqZoCqope_OQbulDzFqL-msIfsxObCTQ7TpWnbxIoAaQoPuL9ge\n5.7.14 BUgbiOqhTEPqJfb02d_L6rrdduHSxv26s_Ztg_JYYavkrqgs85IT1xZYwtbWIRE8OIvQKf\n5.7.14 xxtT7ENlZTS0Xyqnc1u4_MOrBVW8pgyNyeEgKKnKNyxce76JrsdnE1JgSQzr3pr47bL-kC\n5.7.14 XifnWXg> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 fl15sm17237099pdb.92 - gsmtp')
Your code looks correct. Try logging in through your browser and if you are able to access your account come back and try your code again.
Just make sure that you have typed your username and password correct
EDIT:
Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:
Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps
Your code looks correct but sometimes google blocks an IP when you try to send a email from an unusual location. You can try to unblock it by visiting https://accounts.google.com/DisplayUnlockCaptcha from the IP and following the prompts.
Reference: https://support.google.com/accounts/answer/6009563
I have just sent an email with gmail through Python.
Try to use smtplib.SMTP_SSL to make the connection. Also, you may try to change the gmail domain and port.
So, you may get a chance with:
server = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
server.login(gmail_user, password)
server.sendmail(gmail_user, TO, BODY)
As a plus, you could check the email builtin module. In this way, you can improve the readability of you your code and handle emails headers easily.
I want to try hiding my main IP and send email using Hotmail SMTP via Python SMTP Lib. I'm using the following code to send email but its sent from my IP (Originally it sends from Hotmail SMTP but it still shows my IP in the header if can see).
import smtplib
SMTP = "smtp.live.com"
s = smtplib.SMTP(SMTP,587)
s.ehlo()
s.starttls()
s.ehlo()
s.login("username#hotmail.com", "Password")
s.sendmail("username#hotmail.com", "recipient#hotmail.com","Hello World")
Let me know if there's any thing possible from which I can hide my IP and use the proxy IP to send the email. Furthermore, I think playing with socket/socks library might do the trick but still unsure.
Thank you very much.
I am developing my first website. I have page where people can leave message to me. What I am doing is sending a AJAX call to the server, and the server gets the content of the message, then login in an gmail account and send the message to another gmail account using python's email libraries smtplib and email.
It works ok, but the problem is it takes more than one minute on my laptop, the loading image on the client side would keep spinning during the time. I think the bottleneck is login().
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
# login, send email, logout
mailServer.login(conf.user, conf.pw)
mailServer.sendmail(conf.user, to, msg.as_string())
mailServer.close()
So is there any way to speed this up, or other ways to do this.
Thanks
So the delay is probably caused by Google's SMTP server as they do a reverse lookup of the IP address of your webserver - mail systems often do this as part of sending e-mail.
What you have coded is only needed if you intended to send mail out from your account - that is when you need to perform authenticated SMTP. If you want to only mail your own account then you can remove the login step. Note, that you will have to change the mail server from smtp.gmail.com to one of Google's inbound mail (MX) servers. Also, if you are performing a simple forward you will have to handle e-mail rejection or other connection problems so having your script dump the mail to a simple mail server running on the mail server would be helpful.