Python写自动化之邮件发送(匿名)

为了能够实现邮件发送功能,首先,我们需要了解一下邮件的发送过程是什么样子的,此处不再详细说明,请大家自行搜索或查看http://www.sogouqa.com/?p=438

在了解了邮件的发送过程后,为了实现邮件的全自动匿名发送,我们需要通过windows的命令行,解析邮件的匿名服务器,然后使用匿名服务器,发送邮件到相应的邮箱

代码如下:

def _get_mail_exchanger(domain_name,name_server=""):
    #print domain_name
    temp_file=os.getenv("temp")+"\\temp.lsh"
    os.system("nslookup -qt=mx "+domain_name+" "+name_server+" >"+temp_file+" 2>&1")
    f=open(temp_file)
    c=f.read()
    f.close()
    os.remove(temp_file)
    #print c
    internet_addresses_map=_get_internet_address(c)
    mail_exchangers=re.findall(r"mail\s+exchanger\s*=\s*(\S+)",c)
    #mail_exchangers=[internet_addresses_map.get(i,i) for i in mail_exchangers]
    mail_exchangerIPs=[]
    for i in mail_exchangers:
        try:
            mail_exchangerIPs.append(internet_addresses_map[i])
        except:
            ip=_getIP(i)
            if ip:
                mail_exchangerIPs.append(ip)
        
    if mail_exchangerIPs or name_server:
        print "mail_exchangers",mail_exchangerIPs
        return mail_exchangerIPs
    else:
        print "mail_exchangers from 预配置",g_mail_exchanger.get(domain_name,[])
        return g_mail_exchanger.get(domain_name,[])
其中有个函数用于获取匿名邮箱服务器地址,使用正则分析返回值,代码如下:

def _get_internet_address(content):
    internet_addresses=re.findall(r"(\S+)\s+internet\s+address\s*=\s*(\S+)",content)
    internet_addresses_map={}
    for internet_address in internet_addresses:
        internet_addresses_map[internet_address[0]]=internet_address[1]
    return internet_addresses_map

然后,有了地址后,我们需要通过解析出地址的IP,同样,使用windows命令获取结果并正则分析获取IP,代码如下:

def _getIP(domain_name,name_server=""):
    temp_file=os.getenv("temp")+"\\temp.lsh"
    os.system("nslookup -qt=a "+domain_name+" "+name_server+" >"+temp_file+" 2>&1")
    f=open(temp_file)
    c=f.read()
    f.close()
    os.remove(temp_file)
    ips=re.findall(domain_name+r"\s*Address\s*:\s*(\S+)",c,re.I)
    
    if ips:
        return ips[0][0]
    elif name_server=="":
        return _getIP(domain_name,"8.8.8.8")
    else:
        return None

有了邮箱服务器的IP,剩下的事情就是使用IP去发送邮件了,代码比较简单

def _send_to_mail_exchanger(mail_exchanger,mail_from,rcpt_to,From="",To="",Subject="",Date=None,Body="",attachments=None,encoding="gbk"):
    import smtplib,email
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText
    from email.MIMEImage import MIMEImage

    # 构造MIMEMultipart对象做为根容器
    main_msg=email.MIMEMultipart.MIMEMultipart()
    # 设置根容器属性
    main_msg['From'] = From
    main_msg['To'] = To
    main_msg['Subject'] = Subject
    if Date:
	main_msg['Date'] = Date
    else:
	main_msg['Date'] = email.Utils.formatdate()
    
    # 构造MIMEText对象做为邮件显示内容并附加到根容器    
    text_msg = email.MIMEText.MIMEText(Body,'html', encoding)
    main_msg.attach(text_msg)

    # 构造MIMEBase对象做为文件附件内容并附加到根容器  
    if attachments:	
	for attachment in attachments:    
	    if not os.path.isfile(attachment):
		continue
	    if IsImage(attachment):
		try:
		    fp = open(attachment, "rb")
		    file_msg = MIMEImage(fp.read())
		    fp.close()
		    file_msg.add_header("Content-ID", os.path.basename(attachment).replace(".jpg", "").replace(".png", ""))
		    main_msg.attach(file_msg)
		except:
		    pass
	    else:
		file_msg=email.MIMEBase.MIMEBase("application","octet-stream")
		f=open(attachment,'rb')
		file_msg.set_payload(f.read())
		f.close()
		email.Encoders.encode_base64(file_msg)
		file_msg.add_header('Content-Disposition','attachment',filename=os.path.basename(attachment))
		main_msg.attach(file_msg)
    
    # 用smtp发送邮件
    data=main_msg.as_string()
    for i in range(2):
	try:
	    Log(mail_exchanger)
	    server=smtplib.SMTP(mail_exchanger)
	    Log(mail_from)
	    Log(rcpt_to)
	    ret=server.sendmail(mail_from,rcpt_to,data)
	    break
	except:
	    import traceback
	    Log(traceback.format_exc())
	    
	    ret=False
	    try:
		server.quit()
	    except:
		pass
    try:
	server.quit()
    except:
	pass
    
    if ret==False or ret:
	#print "发往如下的邮件失败:",rcpt_to
	return False
    return True
ok,这样把整个流程整理清楚后,就可以自动发送邮件了

转载请注明出处:http://blog.csdn.net/sogouauto


你可能感兴趣的:(测试脚本)