码迷,mamicode.com
首页 > 编程语言 > 详细

python 发邮件的脚本

时间:2019-10-04 18:47:51      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:注意   port   for   int   passwd   start   spl   main   ext   

不加参数,可以 输出帮助 ,及使用方法。

技术图片

 

 

秘送的也是成功的。在收件与抄送 不会显示。

 

 

技术图片

 

 

 

 

# coding: utf-8
import sys
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from smtplib import SMTP
from email.header import Header

def getParsStr(_str):
    return _str[2:len(_str)]

def sendMail(_argv):
    smtp = smtplib.SMTP()
    userName=""
    passWd=""
    hostDomain=""    #smtp.163.com
    jieShouZhe =list()
    chaoSongzhe =list()
    miSongzhe =list()
    title = ""
    fuJian = ""    #写入附件的绝对路径
    zhengWen = ""
    for i in range(1, len(_argv)):
        if _argv[i].startswith(-U):
            userName = getParsStr(_argv[i])
        if _argv[i].startswith(-P):
            passWd = getParsStr(_argv[i])
        if _argv[i].startswith(-D):
            hostDomain = getParsStr(_argv[i])
        if _argv[i].startswith(-R):
            jieShouZhe= getParsStr(_argv[i])
            if len(jieShouZhe) >0:
                jieShouZhe = jieShouZhe.replace("\"","").replace("\‘","").replace("[","").replace("]","").split(",")
        if _argv[i].startswith(-C):
            chaoSongzhe = getParsStr(_argv[i])
            if len(chaoSongzhe) >0:
                chaoSongzhe= chaoSongzhe.replace("\"","").replace("\‘","").replace("[","").replace("]","").split(",")
        if _argv[i].startswith(-S):
            miSongzhe = getParsStr(_argv[i])
            if len(miSongzhe) >0:
                miSongzhe= miSongzhe.replace("\"","").replace("\‘","").replace("[","").replace("]","").split(",")
        if _argv[i].startswith(-T):
            title = getParsStr(_argv[i])
        if _argv[i].startswith(-F):
            fuJian = getParsStr(_argv[i])
        if _argv[i].startswith(-B):
            zhengWen = getParsStr(_argv[i])

    email_client = smtplib.SMTP(hostDomain,25)   #远程主机拒接连接 请使用smtplib.SMTP_SSL()
    email_client.login(userName, passWd)  
    msg = MIMEText(zhengWen, plain, utf-8)
    msg[Subject] = Header(title, utf-8)
    msg[From] = userName
    msg[To] = ,.join(jieShouZhe)
    msg[Cc] = ,.join(chaoSongzhe)
    msg[Bcc] =,.join(miSongzhe)
    email_client.sendmail(userName,jieShouZhe+chaoSongzhe+miSongzhe+[userName],msg.as_string())
    email_client.quit()

def getHelp(_argv):
    if len(_argv)<=1:
        print("""   *** 注意 所有参数不可出现空格 ***
-U 发件者邮箱用户名 eg.463259624@qq.com
-P 发件者邮箱密码
-D 发件者邮箱服务器 eg.smtp.163.com
-R 接收者邮箱,可设置多个格式为[‘xx@xx.com‘,‘yy@yy.com‘]
-C 抄送者邮箱,可设置多个格式为[‘xx@xx.com‘,‘yy@yy.com‘]
-S 密送者邮箱,可设置多个格式为[‘xx@xx.com‘,‘yy@yy.com‘]
-T 邮件标题
-F 附件绝对路径 eg.d:\\test.txt ,(未实现)
-B 邮件内容

实例:
  1). sendMail.py -Utest@test.com -Pmima -Dsmtp.163.com -R‘ShouJianZhe@test.com‘ -T标题 -B邮件内容
  2). sendMail.py -Utest@test.com -Pmima -Dsmtp.163.com -R[‘ShouJianZhe1@test.com‘,‘ShouJianZhe2@test.com‘] -C‘ChaoSongzhe@test.com‘ -S[‘MiSongzhe@test.com‘] -T标题 -B邮件内容
            """)

if __name__ == __main__:
    if len(sys.argv)<=1:
        getHelp(sys.argv)
    else:
        sendMail(sys.argv)

 

python 发邮件的脚本

标签:注意   port   for   int   passwd   start   spl   main   ext   

原文地址:https://www.cnblogs.com/likehc/p/11622699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!