Continue to modify the outgoing mail code of the section above
Send attachments:
(1) Find a local file first
(2) Open file, read out the file string
(3) Create an object att through the Mimtext () class, and pass in the file to read the contents
(4) Add the ATT header information and specify the file name
(5) Add to MSG message Msg.attach (ATT)
Examples:
Attfile = ' test.py ' basename = Os.path.basename (attfile) fp = open (Attfile, ' rb ') att = Email.mime.text.MIMEText (Fp.read () , ' HTML ', ' Utf-8 ') att["content-type"] = ' application/octet-stream ' att.add_header (' content-disposition ', ' Attachment ', filename= (' Utf-8 ', ' ", basename)) #three-tuple of (CharSet, language, value), # encoders.encode_base64 (ATT) Msg.attach (ATT)
Send Picture:
(1) A picture must exist locally;
(2) Open the picture and read the contents of the picture
(3) Create a picture object corresponding to the outgoing mail imgattr = Mimeimage (Fimg.read ())
(4) Add the image header information, Imgattr.add_header (' Content-id ', ' <image1> ')
The ID of the picture is specified, and if the picture is to be displayed in the body, it must be displayed in HTML format: Specify the image ID in the front-end code
Information that is added to the message
So message.conf modified to:
Vim message.conf
From = [email protected]to = [email protected], [email protected]cc = [email protected]Subject = Test Mail File = 1.txt,2.txtimage = 1.jpg ,2.jpgmessage = "Everybody good: test mail test message above Thank you # here is the HTML file information:< H1>hello world
Package The email code, add cc (CC), add Attachment (File) , util.py no changes.
Vim sendmail2.py
Import codecsimport email.mime.multipartimport email.mime.textimport email.headerimport osfrom email.mime.image import mimeimagefrom util import getpropertyimport smtplibclass sendmail (object): def __init__ (self): self.sendfrom = getproperty ("from") self.sendto = getproperty ("to"). Split (",") self.connect = getproperty ("message") Self.sendcc = getproperty ("Cc"). Split (",") Self.sendfile = list () self.sendImage = List () for i in getproperty ("File"). Split (","): self.sendfile.append (I.replace ("\ r", "")) for j in getproperty ("Image"). Split (","): self.sendimage.append (J.replace ("\ r", "")) self.msg = none def setemailheader (self): self.msg = email.mime.multipart.mimemultipart () self.msg[' from '] = self.sendfrom self.msg[' to '] = ";". Join (self.sendto) self.msg[' cc '] = ";". Join (SELF.SENDCC) self.msg[' subject '] = Email.header.Header (GetProperty ("Subject")) def setmessage (self): &NBSP;&NBSP;&NBsp; text = email.mime.text.mimetext (self.connect, ' HTML ', ' Utf-8 ') self.msg.attach (text) def Setfile (self): for file in self.sendfile: if os.path.exists (file): with codecs.open (file , "RB") as f: attr = email.mime.text.mimetext (str (f.read ()), "HTML" , "Utf-8") attr["Content-type"] = ' Application/octet-stream ' &nbsP; attr.add_header (' content-disposition ', ' attachment ', filename= (' Utf-8 ', ', file)) Self.msg.attach (attr) else: print ("{0} file is not exists! ". Format (file)) def setimage (self): Start = 1 for image in self.sendimage: if os.path.exists (image): with Codecs.open (image, "RB") as i: &nbsP; attrimage = mimeimage (I.read ()) attrimage.add_header (' Content-id ', ' <image{0}> '. Format (int (start)) Self.msg.attach (attrimage) start += 1 else: print ("{0} image is no exists!"). Format (image)) def sendemaillast (self): smtp = smtplib. Smtp_ssl ("smtp.qq.com", 465) &NBSP;&NBSP;&NBsp; #smtp. Set_debuglevel (1) #print (Self.sendfrom) smtp.login (self.sendfrom, ' Xrutyyjbcaae ') #print (self.sendto) smtp.sendmail (self.sendfrom, self.sendto + self.sendcc, Self.msg.as_string ()) Def main (): sendmail = sendmail () sendmail.setemailheader () sendmail.setmessage () Sendmail.setfile () sendmail.setimage () sendmail.sendemaillast () if __name__ == ' __main__ ': main ()
File directory of siblings
Execution Result:
$. Python Email (2)