Advertisement
askanton

Отправляем почту с помощью python

Feb 4th, 2022
1,369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import smtplib
  3. from email.mime.image import MIMEImage
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. addr_from = "*********g@gmail.com"
  7. addr_to = "********@gmail.com"
  8. password = "************"
  9. img_data = open("image.jpg", 'rb').read()
  10. msg = MIMEMultipart()
  11. msg['From'] = addr_from
  12. msg['To'] = addr_to
  13. msg['Subject'] = "Тема"
  14. text1 = ("Текст письма")
  15. msg.attach(MIMEText(text1, 'plain'))
  16. #attach photo
  17. try:
  18.     image = MIMEImage(img_data)
  19.     image.add_header('Content-Disposition', 'attachment', filename="image.jpg")
  20.     msg.attach(image)
  21. except Exception as e:
  22.     print(e)
  23.  
  24. smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
  25. smtpObj.starttls()
  26. smtpObj.login(addr_from, password)
  27. smtpObj.send_message(msg)
  28. smtpObj.quit()
  29. print("Mailed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement