Python email needs to master the use of two modules, smtplib and email, these two modules are Python-brought, just import can be used. Smtplib module is mainly responsible for sending mail, email module is mainly responsible for the construction of mail.
Smtplib is easier to use. The following is the most basic syntax.
The import and use methods are as follows:
Import= smtplib. SMTP () smtp.connect ('smtp.163.com,25') smtp.login (username, password) Smtp.sendmail (sender, receiver, msg.as_string ()) Smtp.quit ()
Description
Smtplib. SMTP (): Instantiate SMTP ()
Connect (Host,port):
Host: Specify a connected mailbox server, such as Sina email: smtp.sina.com, netease email: smtp.163.com.
PORT: Specifies the port number of the connection server, which defaults to 25.
Login (User,password):
User: The username of the login mailbox.
Password: Login Email password, like the author is NetEase mailbox, NetEase mailbox is generally web version, need to use the client password, need to be in the web version of the NetEase mailbox set authorization code, the authorization code is the client password.
SendMail (from_addr,to_addrs,msg,...):
From_addr: Mail Sender Address
To_addrs: Mail Recipient address. String list [' Receive address 1 ', ' Receive address 2 ', ' Receive address 3 ',...] or ' Receive Address '
Msg: Send message: Message content.
Quit (): Used to end the SMTP session.
The email module has a MIME package, mime English is all known as "Multipurpose Internet Mail Extensions", that is, multi-purpose Internet Mail extension, is the current Internet e-mail messages generally follow the technical specifications.
There are three modules commonly used under this MIME package: Text,multpart,image.
The import method is as follows:
From Email.mime.multipart import mimemultipart from email.mime.text import mimetext from Email.mime.image Import Mimeimage
Text
Mail sender in order to prevent some mail reading software does not display data processing in HTML format, usually using two types of "Text/plain" and "text/html"
Part1 = Mimetext (text,'plain'utf-8'= Mimetext ( HTML,'html' 'utf-8')
Multpart
There are three types of common multipart: multipart/alternative, multipart/related and multipart/mixed.
Messages with the message type "multipart/alternative" include plain text body (text/plain) and hypertext Body (text/html).
The message body with the message type "multipart/related" includes embedded resources such as pictures, sounds, and so on.
Messages with a message type of "multipart/mixed" contain attachments. Up compatible, if a message has a plain text body, a hypertext body, an inline resource, and an attachment, select the mixed type.
Python automatic e-mail summary-not yet completed