Advertisement
Oleg_Kornilov

Emailing Python + for_cycle (v.1.01)

Apr 10th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 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.  
  11. def sendFileByFile(filename):
  12.    
  13. #список файлов для отправки
  14.     files = ['C://Users/олег/Downloads/f4add3da6a17e50f571809897502e76e.jpg',
  15.              'C://Users/олег/Downloads/stefania-9.jpg',
  16.              'C://Users/олег/Downloads/stefania-ferrario-766802.jpg'
  17.     ]
  18.  
  19.     for i, filename in enumerate(files):
  20.         print(i, filename)
  21.         path = filename
  22.        
  23.         if not os.path.isfile(path):
  24.             print("Неверный путь к файлу")
  25.             continue
  26.         else:
  27.             filename = os.path.basename(path)
  28.            
  29.         basename = os.path.basename(files[i])
  30.     #Receiver email
  31.         receiver_address = 'x'
  32.        
  33.         sender_address = 'z'
  34.        
  35.     #Compose attachment
  36.         part = MIMEBase('application', "octet-stream")
  37.         part.set_payload(open(files[i], "rb").read())
  38.         encoders.encode_base64(part)
  39.         part.add_header('Content-Disposition', 'attachment; filename = "%s"' % basename)
  40.              
  41.     #Compose message
  42.         msg = MIMEMultipart()
  43.     #Header \\ заголовок письма    
  44.         msg['Subject'] = '666'
  45.         msg['From'] = sender_address
  46.         msg['To'] = receiver_address
  47.         msg.attach(part)
  48.  
  49.     #Send mail
  50.         smtp = smtplib.SMTP('smtp.mail.ru', 25)
  51.         smtp.ehlo()
  52.         smtp.starttls()
  53.  
  54.         smtp.login(sender_address, 'sender_email_password')
  55.         smtp.sendmail(sender_address, receiver_address, msg.as_string())
  56.         print('File %s is sent to %s' % (filename, receiver_address))
  57.  
  58.     print('Executed')
  59.     smtp.quit()
  60.        
  61.    
  62.    
  63. sendFileByFile('C://Users/олег/Downloads/2fa813ee34de1793bec65216cb4e3ce3.jpg')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement