Reprinted from: http://www.cnblogs.com/sislcb/archive/2008/12/01/1344861.html
e-mail module to generate email is also very simple, Just need some basic knowledge of mime. Let's look at a bit of MIME basics below.
mime message consists of the message header and the body of the message, which is the message header and the message body. A blank line separates the message header from the message body. This can be done by using a text editor (such as Notepad) to see the source files of a message. Outlook and Foxmail have the ability to view the source files themselves.
message header contains the sender, recipient, subject, Time, MIME version, Important information such as the type of message content. Each piece of information is called a domain, the domain name after the ":" and the information content, can be a row, longer can occupy more than one line. The first line of the field must be "head", that is, the left cannot have white space characters (spaces and tabs), and the continuation line must begin with a blank character, and the first whitespace character is not intrinsic to the information itself.
The message body contains the contents of the message, and its type is from the message header's " Content-type "field indicates. The most common types are text/plain (plain text) and text/html (hypertext). The message body is divided into several segments, each of which contains both the segment head and the segment body, and the two parts are separated by a blank line. There are three types of common multipart: multipart/mixed, multipart/related and multipart/alternative. From their names, it is not difficult to infer the respective meanings and uses of these types. The hierarchical relationships between them can be summarized as follows:
As you can see, if you want to add an attachment in a message, you must define the multipart/mixed segment, or at least define the multipart/related segment if there is an inline resource, or at least define the multipart/alternative segment if the plain text coexists with the hypertext. Generating the message is about generating the various MIME sections. The email module is packaged for these processes and looks at the build method:
1 #-*-encoding:gb2312-*-2 3 ImportEmail4 Importstring, sys, OS, email5 Import Time6 7 classMailcreator:8 def __init__(self):9 #create Message object for mailTenSelf.msg =email. Message.message () OneSelf.mail ="" A - defCreate (self, mailheader, Maildata, mailattachlist=[]): - #Mailheader is a dict type, Maildata is a list, and the first item inside is a plain text type, and the second item is HTML. the #mailattachlist is list, which is the attachment file name - if notMailheaderor notMaildata: - return - + forKinchMailheader.keys (): - #for the subject to be special treatment, Chinese to convert a bit. + #like "One of my test mails" is going to be converted to =?gb2312?b?ztk1xnk7upay4sru08q8/g==?= . A ifK = ='subject': atSELF.MSG[K] = email. Header.header (Mailheader[k],'gb2312') - Else: -SELF.MSG[K] =Mailheader[k] - #Create a plain text section -Body_plain = email. Mimetext.mimetext (Maildata[0], _subtype='Plain', _charset='gb2312') -body_html =None in #Create HTML section, this is optional - ifMaildata[1]: tobody_html = email. Mimetext.mimetext (maildata[1], _subtype='HTML', _charset='gb2312') + - the #Create a multipart, then attach the previous text and HTML sections to the above, and for why, you can look at MIME-related content *Attach=email. Mimemultipart.mimemultipart () $ Attach.attach (Body_plain)Panax Notoginseng ifbody_html: - Attach.attach (body_html) the #Process each attachment + forFNameinchmailattachlist: AAttachment=email. Mimetext.mimetext (email. Encoders._bencode (Open (fname,'RB'). Read ())) the #This sets the file type, all set to application. Of course, it could be image,audio or something, no matter how much. +Attachment.replace_header ('Content-type','application/octet-stream;name= "'+os.path.basename (fname) +'"') - #Be sure to set the transmission encoding to Base64, because this is the default base64 $Attachment.replace_header ('content-transfer-encoding','Base64') $Attachment.add_header ('content-disposition','attachment;filename= "'+os.path.basename (fname) +'"') - Attach.attach (Attachment) - #generate the final message theSelf.mail = Self.msg.as_string () [:-1] +attach.as_string () - Wuyi returnSelf.mail the - if __name__=='__main__': WuMC =Mailcreator () -Header = {' from':'[email protected]',' to':'[email protected]','subject':'One of my test messages'} Aboutdata = ['Plain Text Information','<font color= "Red" >html text information</font>'] $ ifSys.platform = ='Win32': -attach = ['C:\windows\clock.avi'] - Else: -attach = ['/BIN/CP'] A +Mail =mc.create (header, data, attach) the -f = open ("test.eml","WB") $ f.write (mail) theF.close ()
Here I have encapsulated a class to do the processing, the overall process is:
1. Create the Message object first: email. Message.message ()
2. Create Mimemultipart object: Email.MIMEMultipart.MIMEMultipart ()
3. Create individual Mimetext objects and attach them into Mimemultipart, where the mimetext is not just text, but also image, application, audio and so on.
4. Generate the final message.
Python module Email: e-mail encoding