Gmail 发送邮件报警

亲测好使

import smtplib
import email.mime.text
import time

# my test mail
mail_username = 'XXXXXXXXXXX@gmail.com'
password = 'XXXXXXXX'
from_addr = mail_username
to_addr = 'XXXX@qq.com'

smtp_server = 'smtp.gmail.com'


def send_email(title, text):
    # Create SMTP Object
    smtp = smtplib.SMTP()
  
    msg = email.mime.text.MIMEText('{}'.format(text))
    msg['From'] = '操盘小分队 <%s>' % from_addr
    msg['To'] = 'Admin <%s>' % to_addr
    msg['Subject'] = '{}'.format(title)

    server = smtplib.SMTP(smtp_server, 587)  # Gmail的SMTP端口是587
    server.ehlo()
    server.starttls()
    server.set_debuglevel(1)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.quit()

  

posted @ 2019-06-12 15:21  Dream鑫  阅读(269)  评论(0编辑  收藏  举报