Python Send mail
Get ready
In Python, the main use of smtplib and email two modules, the following mainly on the two modules to explain
You need to prepare at least two test mailboxes before you explain them, which you can send and accept by opening the Smtplib protocol in the settings of your mailbox.
Smtplib
-
smtplib.SMTP( [host [, port [, local_hostname[,timeout]]]])hostis the SMTP host server, where the 163 mailbox is smtp.163.com , the other can be found on the Internet, port is the port number, here is the default 25 , local_hostname is your host SMTP , if SMTP on your local machine, You only need to specify the server address to be localhost able. timeoutis the set limit time for the connection, and if it is not connected over this time, an error will occur
-
SMTP.connect([host[, port]]): Connect to the specified smtp server. The parameters represent smpt the host and port, respectively. Note: You can also host specify the port number (for example:) in the parameter smpt.yeah.net:25 so that no parameters are required port .
-
SMTP.login(user, password)Login server, here user is the user name of the mailbox, but here is password not your mailbox password, when you open SMTP will prompt you to set a password, here the password is the corresponding password
-
SMTP.sendmail(from_addr, [to_addrs,], msg[, mail_options, rcpt_options])Send the message, from_addr is the sender is your email address, to_addr is the address of the recipient, of course, here can fill in multiple email accounts sent to multiple accounts, if there are multiple accounts need to use the list to pass parameters
Email
emialThe module is used to process mail messages, including MIME and other based RFC 2822 message documents. Using these modules to define the contents of the message is very simple. The classes it includes are (in more detail, visible: http://docs.python.org/library/email.mime.html):
-
class email.mime.base.MIMEBase(_maintype, _subtype, **_params): This is MIME a base class. You generally do not need to create an instance when you use it. Where _maintype is a content type, such as text or image. _subtype is the type of content minor type , such as plain or gif . **_paramsis a dictionary that is passed directly to Message.add_header ().
-
class email.mime.multipart.MIMEMultipart([_subtype[, boundary[, _subparts[, _params]]]]: MIMEBase a sub-class, a MIME collection of multiple objects, and a _subtype default value of mixed . boundaryis MIMEMultipart the boundary, and the default boundary is a number. This class is used when you need to send an attachment.
-
class email.mime.application.MIMEApplication(_data[, _subtype[, _encoder[, **_params]]]): MIMEMultipart a subclass of.
-
class email.mime.audio. MIMEAudio(_audiodata[, _subtype[, _encoder[, **_params]]]): MIME Audio Object
-
class email.mime.image.MIMEImage(_imagedata[, _subtype[, _encoder[, **_params]]]): A binary MIME file object. Mainly used to send pictures
Plain Text Mail
-
class email.mime.text.MIMEText(_text[, _subtype[, _charset]]): MIME text object, which _text is the message content, _subtype message type, can be text/plain (plain text mail), html/plain (HTML mail), _charset encode, can be and gb2312 so on.
-
The key to the implementation of plain text mail delivery is MIMEText to _subtype set the plain . First import smtplib and mimetext . Create smtplib.smtp instance, connect mail smtp server, login send after, specific code as follows *
# A function that formats the message can be used to use Def _format_addr (s): name, addr = parseaddr (s) return formataddr ( Header (name, ' Utf-8 '). Encode (), addr.encode (' utf-8 ') if isinstance (addr, Unicode) Else addr)) from_addr= ' xxxxxxxx ' # Your email address from_password= ' xxxxxxx ' #你的密码 # to_email= ' chenjiabing666@yeah.net ' to_email= ' xxxxxx ' #要发送的邮箱地址msg = Mimetext (' disguised, unscrupulous ', ' plain ', ' utf-8 ') #这里text = disguised, unscrupulous msg[' from '] = _format_addr (U ' python enthusiast <%s> '% FROM_ADDR) #格式化发件人msg [' to '] = _format_addr (U ' admin <%s> '% to_email) #格式化收件人msg [' Subject '] = Header ( U ' Greetings from SMTP ... ', ' utf-8 '). Encode () #格式化主题stmp = ' smtp.163.com ' server=smtplib. SMTP (stmp,port=25,timeout=30) #连接, set the time-out 30sserver.login (From_addr,from_password) #登录server. Set_debuglevel ( 1) #输出所有的信息server. SendMail (From_addr,to_email,msg.as_string ()) #这里的as_string () is to convert msg into a string type, If you want to send more than one person, you need to tell To_email to a list
Send an HTML message
Still used MIMEText to send, but the settings in the _subType HTML can be, the detailed code is as follows:
def _format_addr (s): name, addr = parseaddr (s) return formataddr ( Header (name, ' Utf-8 '). Encode (), Addr.encode (' Utf-8 ') if isinstance (addr, Unicode) Else addr)) from_addr= ' xxxxxxxx ' #你的邮箱地址from_password = ' xxxxxxx ' #你的密码 # to_email= ' chenjiabing666@yeah.net ' to_email= ' xxxxxx ' #要发送的邮箱地址html = "" <p>
Sending of attachments
Send the message with attachments, first to create an MIMEMultipart() instance, and then construct the attachment, if there are multiple attachments, can be constructed in turn, the final use smtplib.smtp of transmission, the specific strength is as follows:
From Email.mime.multipart import mimemultipartfrom email.mime.text import mimetextimport smtplibfrom email.mime.image Import mimeimagefrom email.mime.multipart import mimemultipartfrom email.mime.text import Mimetextfrom Email.header Import Headerdef _format_addr (s): name, addr = parseaddr (s) return Formataddr ((Header (name, ' Utf-8 '). Encode ( ), Addr.encode (' Utf-8 ') if isinstance (addr, Unicode) Else addr)) from_addr= ' xxxxxxxx ' #你的邮箱地址from_password = ' xxxxxx X ' #你的密码 # to_email= ' chenjiabing666@yeah.net ' to_email= ' xxxxxx ' #要发送的邮箱地址msg =mimemultipart () #创建实例text =mimetext (' < ; H2 style= "color:red" > Chen Ga Bing
Embed a picture in the body information
From Email.mime.multipart import mimemultipartfrom email.mime.text import mimetextimport smtplibfrom email.mime.image Import mimeimagefrom email.mime.multipart import mimemultipartfrom email.mime.text import Mimetextfrom Email.header Import Headerdef _format_addr (s): name, addr = parseaddr (s) return Formataddr ((Header (name, ' Utf-8 '). Encode ( ), Addr.encode (' Utf-8 ') if isinstance (addr, Unicode) Else addr)) from_addr= ' xxxxxxxx ' #你的邮箱地址from_password = ' xxxxxx X ' #你的密码 # to_email= ' chenjiabing666@yeah.net ' to_email= ' xxxxxx ' #要发送的邮箱地址msg =mimemultipart () #创建实例html = "" "<HTML&G T;