import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart mailto_list = ["to_name@163.com"] mail_host = "smtp.163.com" mail_user = "user_name@163.com" mail_pass = "password" # mail_subject = "python發送郵件測試" # 郵件的標題 # mail_context = "這是郵件內容-----XXOO" def send_main(mail_subject, Filename): msg = MIMEMultipart() msg["From"] = mail_user # 發件人 msg["To"] = ";".join(mailto_list) # 收件人 msg["Subject"] = mail_subject # 郵件標題 # 郵件正文 with open(Filename, "rb") as f: fc = f.read() txt = MIMEText(fc, 'html', 'utf-8') msg.attach(txt) # 構造附件 att = MIMEText(open(Filename, "rb").read(), "base64", "utf-8") att["Content-Type"] = "application/octet-stream" # 附件名稱為中文時的寫法 att.add_header("Content-Disposition", "attachment", filename=("gbk", "", "5.3版本接口詳細測試結果.html")) # 附件名稱非中文時的寫法 # att["Content-Disposition"] = 'attachment; filename="test.html")' print(1) msg.attach(att) smtp = smtplib.SMTP() smtp.connect(mail_host) smtp.login(mail_user, mail_pass) smtp.sendmail(mail_user, mailto_list, msg.as_string()) smtp.quit() print("郵件發送成功")
