python自動發送郵件配置

import smtplib
from email.mime.text import MIMEText
def send_mail(mail_from, mail_to, subject, msg):
    try:
        mail_host = "xxxxxx" # 郵箱服務器地址
 s = smtplib.SMTP(mail_host)
        s.connect(mail_host)
        s.sendmail(mail_from, mail_to, msg.as_string())
        s.close()
        return True
 except Exception as e:
        print(str(e))
        return False

使用:

from_mail = "[email protected]"
to_mail = "[email protected]"
subject = "test_auto_email"
msg = MIMEText(html_msg, 'html', 'utf-8')      # 發送的是html格式的
msg['Subject'] = '[測試]'
msg['From'] = from_mail        # 顯示發件人
msg['To'] = to_mail           # 顯示收件人
send_mail(from_mail, to_mail, subject, msg)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章