Advertisement
Oleg_Kornilov

Few Files Email Python

Apr 10th, 2016
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 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. files = ['C://Users/олег/Downloads/2fa813ee34de1793bec65216cb4e3ce3.jpg',
  11.          'C://Users/олег/Downloads/2dcf856c4a33ca36eed01940b5d33679.jpg',
  12.          'C://Users/олег/Downloads/programmiruem-na-python-3-e-izdanie.zip',
  13.          'C://Users/олег/Downloads/stefania-ferrario-766802.jpg'
  14. ]
  15.  
  16. def sendFileByFile(filename):
  17.     for filename in files:
  18.         path = filename
  19.         if not os.path.isfile(path):
  20.             raise ("Неверный путь к файлу")
  21.         else:
  22.             filename = os.path.basename(path)
  23.            
  24.         basename = os.path.basename(files[0])
  25.         address = 'z'
  26.  
  27.     #Compose attachment
  28.         part = MIMEBase('application', "octet-stream")
  29.         part.set_payload(open(files[0], "rb").read())
  30.         encoders.encode_base64(part)
  31.         part.add_header('Content-Disposition', 'attachment; filename = "%s"' % basename)
  32.                  
  33.     #Compose message
  34.         msg = MIMEMultipart()
  35.         msg['From'] = 'z'
  36.         msg['To'] = 'z'
  37.         msg.attach(part)
  38.  
  39.     #Send mail
  40.         smtp = smtplib.SMTP('smtp.mail.ru', 25)
  41.         smtp.ehlo()
  42.         smtp.starttls()
  43.  
  44.         smtp.login(address, 'sender_email_password')
  45.         smtp.sendmail(address, address, msg.as_string())
  46.         print('File %s is sent' % filename)
  47.         smtp.quit()
  48.        
  49. sendFileByFile('2fa813ee34de1793bec65216cb4e3ce3.jpg')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement