Linux Python send mail

need:

After running the experiment on the linux server, an email reminder is automatically sent

accomplish:

smtplib and QQ mailbox in python3 (you need to go to QQ mailbox to obtain the authorization code, just click Baidu)

code:

# file name: dmlemail.py
# 千万不要命名为email.py
import smtplib
from email.mime.text import MIMEText
import sys

MAIL_USER = "[email protected]" # 用于发送通知的邮箱
MAIL_PWD = "xxxxxxxxxxx"	# 该邮箱的授权码

def mail(mail_subject, mail_text, mail_to):
    msg = MIMEText(mail_text)
    msg['Subject'] = mail_subject
    msg['From'] = MAIL_USER
    msg['To'] = mail_to

    send = smtplib.SMTP_SSL("smtp.qq.com",465)
    send.login(MAIL_USER, MAIL_PWD)
    send.send_message(msg)

    send.quit()

def main(argv):
    mail_subject= 'experiment: '+ argv[1]+' finished.'
    mail_text='A new wonderful day!'
    #自己发给自己
    mail_to = MAIL_USER
    mail(mail_subject, mail_text, mail_to)
    print('email sent')

if __name__ == '__main__':
    main(sys.argv)


use:

Just fill in the next line of the experiment script, so that an email reminder will be automatically sent after the experiment is finished!

python dmlmail.py experiment_name

 

Guess you like

Origin blog.csdn.net/daimaliang/article/details/115868330