Advertisement
Oleg_Kornilov

Python 3x File Emailing

Apr 8th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. import smtplib
  2. from smtplib import SMTP_SSL
  3. import email
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.base import MIMEBase
  6. from email.mime.text import MIMEText
  7. from email import encoders
  8. import os
  9.  
  10. #filepath must be like this (from the very beginning directory): 'C://dir/dir/dir/dir/filename.jpg' or any other
  11. #filename extension. for example .doc
  12. filepath = 'C://Users/олег/Downloads/long_legs_by_carolelalibre-d5l5r75.jpg'
  13. basename = os.path.basename(filepath)
  14. address = 'sender_email'
  15.  
  16. #Compose attachment
  17. part = MIMEBase('application', "octet-stream")
  18. part.set_payload(open(filepath, "rb").read())
  19. encoders.encode_base64(part) #i think this '64' stuff stands for your Win64 or 32, i dont know for sure
  20. part.add_header('Content-Disposition', 'attachment; filename = "%s"' % basename)
  21.                
  22. #Compose message
  23. msg = MIMEMultipart()
  24. msg['From'] = 'sender_email'
  25. msg['To'] = 'receiver_email'
  26. msg.attach(part)
  27.  
  28. #Send mail
  29. smtp = smtplib.SMTP('smtp.mail.ru', 25) #25 is the port of smtp.mail.ru
  30. smtp.ehlo()
  31. smtp.starttls()
  32.  
  33. smtp.login(address, "sender's_email_password")
  34. smtp.sendmail(address, address, msg.as_string())
  35. print('Executed')
  36. smtp.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement