Здравствуйте, есть скрипт, он посылает мыло и делает заголовок из 85 символов строки файла.
начиная с первой.
как сделать, чтобы он выбирал рандомные, не по порядку?
import smtplib
import random
from smtplib import SMTP_SSL
import email
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import os
import sys, traceback
import time
from time import gmtime, strftime
c = 0
files = [
'C://Users//test//Downloads//1_Amanda-De-Cadenet.jpg',
'C://Users//test//Downloads//0_a82a7_7db42dad_orig.jpg',
'C://Users//test//Downloads//GildaAmbrosio-1.jpg',
'C://Users//test//Downloads//gilda20140223-4844-2.jpg'
]
answers = ['y', 'да', 'YES', 'yes', 'Y']
sender_address = 'test.test.12@bk.ru'
receiver_address = 'test.test.12@bk.ru'
#открываем файл
f = open("C:\\Users\\test\\Desktop\\spam.txt")
def send():
global c
date = strftime("%H:%M:%S")
try:
while True:
for i, filename in enumerate(files):
path = random.choice(files)
if not os.path.isfile(path):
print("Неверный путь к файлу")
continue
else:
filename = os.path.basename(path)
basename = os.path.basename(files[i])
#Receiver email
sender_address = 'test.test.12@bk.ru'
#Compose attachment
part = MIMEBase('application', "octet-stream")
part.set_payload(open(files[i], "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename = "%s"' % basename)
#Compose message
msg = MIMEMultipart()
#Header \\ заголовок письма
msg['Subject'] = f.readline()[0:85] #строка создающая заголовок
msg['From'] = sender_address
msg['To'] = receiver_address
msg.attach(part)
#Send mail
smtp = smtplib.SMTP('smtp.mail.ru', 25)
smtp.ehlo()
smtp.starttls()
smtp.login(sender_address, 'test')
smtp.sendmail(sender_address, receiver_address, msg.as_string())
c += 1
print('%s sent to: %s ' % (filename, receiver_address), c, '\n')
except Exception:
print(traceback.format_exception(*sys.exc_info())[1])
input("End ")
def sendFileByFile():
for i, filename in enumerate(files):
path = filename
filename = os.path.basename(path)
global c
global sender_address
r_address = input("спамить на мыло: %s\nфайлом %s? --->>> " % (receiver_address, filename))
if r_address in answers:
send()
if not r_address:
send()
sendFileByFile()