Python发邮件脚本

收集的一个Python发邮件脚本,实现在不搭建邮件服务器的环境下,通过调用其它邮箱发送邮件。

vim mail.py

内容如下:

#!/usr/bin/env python
#-*- coding: UTF-8 -*-

import os,sys
reload(sys)
sys.setdefaultencoding('utf8')
import getopt
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from subprocess import *
def sendqqmail(username,password,mailfrom,mailto,subject,content):
    gserver = '邮件服务器'
    gport = 25
    try:
        msg = MIMEText(unicode(content).encode('utf-8'))
        msg['from'] = mailfrom
        msg['to'] = mailto
        msg['Reply-To'] = mailfrom
        msg['Subject'] = subject
        smtp = smtplib.SMTP(gserver, gport)
        smtp.set_debuglevel(0)
        smtp.ehlo()
        smtp.starttls()
        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()

基本格式:

chmod +x mail.py
./mail.py 收件人 邮件标题 邮件内容

标签: 脚本

添加新评论