使用 python 脚本实现发邮件功能

lance 2019-09-18 PM 86℃ 0条
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys
import getopt
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from subprocess import *
reload(sys)
sys.setdefaultencoding('utf8')

def sendqqmail(username, password, mailfrom, mailto, subject, content):
    gserver = 'smtp.exmail.qq.com'
    gport = 465

    try:
        # msg = MIMEText(unicode(content).encode('utf-8')) //如果发送的邮件有乱码,可以尝试把这行改成如下:
        msg = MIMEText(content, 'plan', 'utf-8')
        msg['from'] = mailfrom
        msg['to'] = mailto
        msg['Reply-To'] = mailfrom
        msg['Subject'] = subject

        smtp = smtplib.SMTP_SSL(gserver, gport)
        smtp.set_debuglevel(0)
        smtp.ehlo()
        smtp.login(username, password)

        smtp.sendmail(mailfrom, mailto, msg.as_string())
        smtp.close()
    except Exception, err:
        print "Send mail failed. Error: %s" % err

def main():
    to = sys.argv[1]
    subject = sys.argv[2]
    content = sys.argv[3]
    sendqqmail('发件邮箱地址', '授权码', '发件邮箱地址', to, subject, content)

if __name__ == "__main__":
    main()
标签: linux, 发邮件, python

非特殊说明,本博所有文章均为博主原创。

觉得文章不错,打赏一点吧,1分也是爱😀

WeChat Pay

微信打赏

Alipay

支付宝打赏

评论啦~