Using Gmail Through Python Authentication Error - python

*EDIT: SOLVED IT (answer below)
I'm getting an authentication error 534 from gmail when I try to run the following code:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("me#gmail.com", "password")
msg = "Hello from the other side!"
server.sendmail("me#gmail.com", "me#gmail.com", msg)
server.quit()
return 'Message Sent'
I also noticed that google picked up the login attempt and flagged it as a suspicious device because I'm runnning the code from a server on AWS in another state... however, I flagged it as my device, and I'm still getting the same error. What should I do?
*I also DID turn on access for less secure apps to login.

After telling google that it WAS my device, I just had to do this before the changes could propagate!
https://accounts.google.com/DisplayUnlockCaptcha
Problem = Solved

Related

Python Script for sending mail returns no errors but mail not received

Using my gmail account as a test for sending mail using python. It appears the mail is getting sent (or at least passes over the mail server) but it is not being received. I cannot seem to find any logs that would allow me to trace the email (Using Linux Mint 20). First question is where can I find these logs? Secondly, does anything in this script seem incorrect? Please assume that the data is correctly populated. My gmail account is set to allow less secure apps as well.
with open("config.json",) as config:
data = json.load(config)
smtp_server = data["MAIL"]["SMTP_SERVER"]
port = data["MAIL"]["SMTP_PORT"]
sender_email = data["MAIL"]["SMTP_ACCOUNT"]
password = data["MAIL"]["SMTP_PASSWORD"]
context = ssl.create_default_context()
try:
server = smtplib.SMTP(smtp_server,port)
server.ehlo()
server.starttls(context=context)
server.ehlo()
server.login(sender_email,password)
return server
except Exception as e:
print(e)
return None

Sending e-mail with a Python script deployed on a Linode server using the SMTP library

So I've created a script which sends an update e-mail when the script is finished running. It all works fine locally. But when deployed and ran on a Linode server it doesn't work anymore. Here is the code:
def email(products, new_products, updated_products):
#print(products, new_products, updated_products)
message = EmailMessage()
message.set_content("Products scraped: {}, New products: {}, Updated products: {}".format(products, new_products, updated_products))
message['FROM'] = "e-mail"
message['TO'] = ["e-mail"]
message['SUBJECT'] = "Update"
context = ssl.create_default_context()
#set up SMTP server
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.starttls(context=context)
smtp.login(message['FROM'], "password")
smtp.send_message(message)
smtp.quit()
The error occurs on the line: with smtplib.SMTP('smtp.gmail.com', 587) as smtp. I've tried different ports, but it all results in this error: TimeoutError: [Errno 110] Connection timed out. Has anyone had this problem? Or does anyone have another good way to send an e-mail from a server? Thanks in advance!
Linode block outgoing mail by default to prevent spam.
https://www.linode.com/community/questions/19082/i-just-created-my-first-linode-and-i-cant-send-emails-why-mailing-ports-25-465-a
Try enabling less secure apps for the email id you want to send emails from

Sending mail with python SMTP to outlook/microsoft gives SMTPAuthenticationError: Authentication unsuccessful

I have a problem described and solved for gmail in this post. I have read through several posts dealing with similar issues (Sending mail from Python using SMTP, Sending email from Python using STARTTLS, smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')) and also some external support on Microsoft (eg. send email using Microsoft 365 but in none I was able to figure out how to fix my error.
My code is this:
import getpass
import smtplib
mailserver = smtplib.SMTP('smtp.office365.com',587)
mailserver.ehlo()
mailserver.starttls()
a = getpass.getpass('Password: ')
mailserver.login('name#domain.com', a)
and connection is established (ie. I get 250 for .ehlo and 220 for .starttls) but the last step gives me:
SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful [LO4P123CA0352.GBRP123.PROD.OUTLOOK.COM]')
The documentation says:
SMTP authentication went wrong. Most probably the server didn’t accept the username/password combination provided.
I am sure that my password and username are correct (triple checked that). Also, I looked to my account and in the Microsoft account it shows that POP and IMAP are allowed and it gives me:
Server name: smtp.office365.com
Port: 587
Encryption method: STARTTLS
so everything should be ok. I have also tried port 25 which gives the same error. Where is the problem? How can I make it work for Outlook/Microsoft 365? Thanks for any help.

Python smtplib hangs when sendmessage() is called

I'm trying to send a message from a gmail account using smtplib. I've been messing with it in the shell, and everything works fine until I call sendmail(), when it just freezes until I close the terminal. No errors, just freezing. My commands look like this:
>>> import smtplib
>>> connection = smtplib.SMTP("smtp.gmail.com", 587)
>>> connection.ehlo()
>>> connection.starttls()
>>> connection.login("gmail_id", "gmail_pwd")
>>> connection.sendmail("addr_from", "addr_to", "message")
Has anybody had this problem? I've tried pinging smtp.gmail.com and using telnet smtp.gmail.com 587, and they both connect.
Use connection.set_debuglevel(1) to enable tracking/debugging SMTP session.
https://docs.python.org/2/library/smtplib.html#smtplib.SMTP.set_debuglevel
I had a similar issue just now, I had tried to login to my gmail account using python3.7's smtplib and was getting:
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')
I went to my gmail inbox using a browser, saw that I received a security alert email with the message
Less secure app blocked: Google blocked the app you were trying to use
because it doesn't meet our security standards.Some apps and devices
use less secure sign-in technology, which makes your account more
vulnerable. You can turn off access for these apps, which we
recommend, or turn on access if you want to use them despite the
risks. Google will automatically turn this setting OFF if it's not
being used.
After I followed the instructions in the aforementioned email and enabled less secure apps access on my gmail account, I tried sendmail still using the initial SMTP connection, but it just hung with no output.
I then proceeded to quit the initial SMTP connection and created a new one (post less secure apps enabled) like below and it worked!
conn = smtplib.SMTP('smtp.gmail.com', 587)

Python smtplib Tmobile is blocking

I am trying to send a message with python using the smtplib library.
Here is the code I am using:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(EMAIL, PASS)
server.sendmail(EMAIL, phone_number +"#"+ carrier_address, msg)
server.quit()
I am using the port 465 to use a secure connection when connecting, but this still gets the message blocked when sending to a tmobile phone number. It seems that any other carrier works.
Has anyone else run into this issue? My website depends on these texts, and they have to be on time, so this is a very bad bug.
Is there a workaround to this or any other library out there to make this work?
Assuming you are getting the 550 block message from T-Mobile, I found this discussion that goes over the problem. Ths gist is downwards, but here's the main quote:
Sender Policy Framework 550 status message is part of spam validation screening. "550" is part of a long list of SMTP status codes to help diagnose connection issues. Mail relays connecting to #tmomail.net DNS and MX (mail) records must match or be authorized on behalf of another domain. The mismatch can occur in the registered domain or the shared ISP network connection.
I don't know how to update DNS or MX records as I am just a humble pythoner trying to text some folks. If you worked through your error already, what worked for you? Thanks.

Categories

Resources