In order to be able to send the message, first of all, we need to know what the message is sent out of the process, no longer detailed here, please do their own search or view http://www.sogouqa.com/?p=438
After understanding the message sending process, in order to realize the automatic mail anonymous send, we need to parse the message's anonymous server through the Windows command line, then uses the anonymous server, sends the mail to the corresponding mailbox
The code is as follows:
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.rea D () 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_exchanger IPs Else:print "Mail_exchangers from Pre-Configuration", G_mail_exchanger.get (domain_name,[]) return G_MAIL_EXCHANGER.G ET (domain_name,[])
There is a function to get the anonymous mailbox server address, using the regular parse return value, the code is as follows:
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
Then, with the address, we need to parse out the address of the IP, similarly, using the Windows command to get the results and regular analysis to obtain the IP, the code is as follows:
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
With the mailbox server IP, the rest of the matter is to use IP to send mail, the code is relatively simple
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 # Constructs Mimemultipart objects as root container main_msg=email. Mimemultipart.mimemultipart () # Set the root container property main_msg[' from '] = from main_msg[' to '] = to main_msg[' Subject '] = subj ECT if date:main_msg[' date '] = date else:main_msg[' Date ' = email. Utils.formatdate () # Constructs a Mimetext object as a message display and attaches to the root container text_msg = email. Mimetext.mimetext (Body, ' HTML ', encoding) Main_msg.attach (text_msg) # constructs Mimebase objects as file attachment contents and attaches to the root container if attachments : For attachment in Attachments:if not os.path.isfile (attachment): Continue if Isimage (attachment): TRY:FP = O Pen (attachment, "RB") File_msg = Mimeimage (Fp.read ()) Fp.close () File_msg.add_header ("Content-id", Os.path.basena Me (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) # Send mail with 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) breakexcept:i Mport Traceback Log (Traceback.format_exc ()) Ret=false try:server.quit () Except:pass try:server.quit () Except:pass if Ret==false or ret: #print "message to the following failed:", Rcpt_toreturn False return True
OK, so that the entire process is clear, you can automatically send mail
Reprint Please specify source: Http://blog.csdn.net/sogouauto
Python write automated Mail sending (anonymous)