Python | SMTP,明文、TLS、SSL一次完成
近期有利用到 Python 透過 smtplib 發送電子郵件,因此寫了個簡單的 class 方便使用。 import smtplib from os.path import basename from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.header import Header from email.utils import formatdate import html class SendMail: def __init__(self, send, receiver, subject, body, files): self.mail = MIMEMultipart() self.mail['Date'] = formatdate(localtime=True) # 設定發信時間 self.mail['Subject'] = subject # 設定信件標題 self.mail.attach(MIMEText(html.unescape(body), 'html', 'utf-8')) # HTML解碼 for f in files or []: # 載入附件 with open(f, "rb") as fil: part = MIMEApplication( fil.read(), Name=basename(f) ) # After the file is closed part['Content-Disposition'] = 'attachment; filename="%s"...