Как отправить несколько файлов в smtplib Python? - PullRequest
0 голосов
/ 25 апреля 2020

Прошу прощения за неработающий язык Как отправить несколько файлов в smtplib Python? Я могу отправить один или два файла в одном письме, но я хочу отправить 7-8 файлов в одном письме.

Это мой код для отправки двух файлов в одном письме:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path

email = 'myemail'
password = 'mypassword'
send_to_email = 'send_to_email'

subject = 'This is the subject'
message = 'This is my message'

file_location = 'file-one.txt'
file_locationt = 'file-two.txt'

msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
filename = os.path.basename(file_location)

attachment = open(file_location, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

filenamet = os.path.basename(file_locationt)
attachmentt = open(file_locationt, "rb")
partt = MIMEBase('application', 'octet-stream')
partt.set_payload(attachmentt.read())
encoders.encode_base64(partt)
partt.add_header('Content-Disposition', "attachment; filename= %s" % filenamet)
msg.attach(part)
msg.attach(partt)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
...