1. 程式人生 > >python3使用stmplib發送郵件

python3使用stmplib發送郵件

word 內容 pass connect ret 郵件內容 可能 stmp tex

代碼如下:

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formataddr

# 第三方 SMTP 服務
mail_host = "smtp.exmail.qq.com"
mail_port = 465  # 也可能是25,看情況。SMTPS的話一般是465
mail_user = "[email protected]"
mail_pass = "aaa"   # 密碼或郵箱授權碼

mail_sender 
= [email protected] mail_receivers = [[email protected]] subject = 郵件主題 message = MIMEText(郵件內容, plain, utf-8) # plain表示郵件類型 message[From] = formataddr(["發件人別稱", "[email protected]"]) message[To] = formataddr(["收件人別稱", "[email protected]"]) message[
Subject] = Header(subject, utf-8) def sendmail(host, port, sender, password, recvivers, meassage): try: mail_obj = smtplib.SMTP_SSL() # 郵件服務器使用 SMTPS 的時候用 SMTP_SSL mail_obj.connect(host, port) mail_obj.login(sender, password) mail_obj.sendmail(sender, recvivers, meassage.as_string()) mail_obj.quit()
print("Send sucessful") return True except Exception as e: err = "Send Failed for reason:\n {}".format(e) print(err) return False sendmail(mail_host, mail_port, mail_sender, mail_pass, mail_receivers, message)

python3使用stmplib發送郵件