[Python] 發送含附件email

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


class Send_Email:

    def send_email(self, email_content, me, email_server, smtp_port, user_name, password, email_to,
                   SUBJECT, attachments=None,
                   use_tls=True):
        msg = MIMEMultipart()
        msg['Subject'] = SUBJECT
        msg['From'] = me
        msg['To'] = ', '.join(email_to)
        msg.attach(MIMEText(email_content))
        if attachments is not None:
            for attachment in attachments:
                part = MIMEBase('application', "octet-stream")
                with open(attachment, 'rb') as file:
                    part.set_payload(file.read())
                encoders.encode_base64(part)
                part.add_header('Content-Disposition', f'attachment; filename={os.path.basename(attachment)}')
                msg.attach(part)
        server = smtplib.SMTP()
        server.connect(email_server, smtp_port)
        if use_tls:
            server.starttls()
        server.login(user_name, password)
        server.sendmail(me, email_to, msg.as_string())
        server.quit()