0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

python gmail send mail

Posted at

https://myaccount.google.com/security
Allow 2 step add app password
2.
https://accounts.google.com/DisplayUnlockCaptcha

import os
import smtplib

def sent_gmail(user, password, send_to, subject=None, body=None):
   if isinstance(send_to, list):
      send_to = ', '.join(send_to)
   email_text = f'From: {user}\nTo: {send_to}\nSubject: {subject}\n\n{body}'

   try:
      server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
      server.ehlo()
      server.login(user, password)
      server.sendmail(user, send_to, email_text)
      server.close()
      return True, 'Success'
   except Exception as e:
      return False, str(e)

if __name__ == "__main__":
   user = "youremail@gmail.com"
   password = "apppassword"
   to = 'youremail@gmail.com'
   result, reason = sent_gmail(user, password, to)

   print(result, reason)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?