Sending letters in strange python behavior

I am working on a piece of code that regularly sends emails from 4 different accounts, of which 2 are gmail accounts and the other 2 are Yahoo accounts. When I started writing code, I was able to send all emails from both gmail accounts using the following code snippet:

def sendGmail(self, fromaddr, toaddr, username, password, email_body, email_subject ): # Build the email msg = MIMEText(email_body) msg['Subject'] = email_subject msg['From'] = fromaddr msg['To'] = toaddr try: # The actual mail send server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddr, msg.as_string()) server.quit() print "email sent: %s" % fromaddr except Exception as e: print "Something went wrong when sending the email %s" % fromaddr print e 

As I said, this piece of code worked fine, now when I add sendYahoomail (), which is another method, I always get (530, "Access denied") as an exception for using sendGmail (). I am sure this has nothing to do with my Yahoo method, and I can correctly enter the browser with gmail credentials.

What could be wrong, or just Gmail doesn't want me to send the code?

+1
source share
2 answers

I tried my code above with my Gmail account (it’s actually a Google Apps account, but it should be identical) and it works fine for me. I even tried the wrong "From" header and also used the wrong source address at the SMTP level - both times Gmail allowed me to send email, but it seemed to calmly fix the header on exit. However, you may need to re-verify your address on your username. Also try using the full email address as the username if you are currently only using the part before @.

I also tried the wrong password and received error 535, which does not match what you see. Since I have two-factor authentication enabled, I also tried my real password instead of a specific application and still generated error 535 (but with the message “password for the application is required”).

Is it possible that your ISP has set up something that intercepts SMTP connections to Gmail? It seems unlikely that although my ISP once blocked access to Gmail on port 587, although port 465 still worked. Perhaps you could try using smtplib.SMTP_SSL on port 465 just in case and see if that gives you more joy.

You can also try sending addresses to different providers if Gmail rejects sending for this reason (for example, if another provider was included in the spam blacklist, for example). In addition, if possible, your e-mail looks like spam, try entering a code with a message subject and body that are close to a genuine email and see if that helps.

+2
source

You may find it helpful: https://support.google.com/mail/answer/14257

Basically, Google detected an attempt to log in from a server that it considers unfamiliar, and blocked it. The above link allows you to unlock it.

Sometimes it worked for me, and sometimes it didn’t.

+1
source

Source: https://habr.com/ru/post/944833/


All Articles