0

This code puts files in email body-it works fine.How can i modify this code to attach multiple attachments ? I'm not familiar with python

#!/usr/bin/python3

import smtplib
from email.mime.text import MIMEText


def read_files(file_list):
    data = ''
    for filename in file_list:
        with open(filename) as file:
            data += file.read()
    return data


def send_mail(header, body):
    msg = MIMEText(body, 'html')  # second parameter is MIME type
    msg['Subject'] = header['Subject']
    msg['From'] = header['From']
    msg['To'] = header['To']
    s = smtplib.SMTP('localhost')
    s.send_message(msg)
    s.quit()


def main():
    file_list = ['1.html', '2.html']
    data = read_files(file_list)
    header = {'To': 'user@company.com', 'Subject': ' AWS Reports' , 'From': 'user@company.com'}
    send_mail(header, data)

main()

in def send_mail

i added

part = MIMEBase('application', "octet-stream")
    part.set_payload(open("output.json", "rb").read())
    Encoders.encode_base64(part)

    msg.attach(part)

but got:

'Cannot attach additional subparts to non-multipart/*')
email.errors.MultipartConversionError: Cannot attach additional subparts to non-multipart/*

I found this solution but message is encoded:https://www.tutorialspoint.com/python3/python_sending_email.htm

Milister
  • 648
  • 1
  • 15
  • 33

0 Answers0