Python Learning Notes-mail module SMTP

Source: Internet
Author: User

Smtplib module:

The SMTP (Simple Mail Transfer Protocol) is simply the message Transfer Protocol, which is a set of rules for sending mail from the source address to the destination, and it controls the way the letters are relayed.

Python's Smtplib provides a convenient way to send e-mails. It provides a simple encapsulation of the SMTP protocol.

Python creates SMTP object syntax as follows:

Import smtplibsmtpobj = Smtplib. SMTP ([host [, Port [, Local_hostname]])

Parameter description:

    • HOST:SMTP Server host. You can specify the IP address of the host or the domain name such as:, which is an optional parameter.

    • PORT: If you provide the host parameter, you need to specify the port number that the SMTP service uses, and the SMTP port number is typically 25.

    • Local_hostname: If SMTP is on your local computer, you only need to specify the server address as localhost.

P the Ython SMTP object uses the SendMail method to send messages with the following syntax:

Smtp.sendmail (From_addr, To_addrs, msg[, Mail_options, Rcpt_options]

Parameter description:

    • From_addr: Mail Sender address.

    • To_addrs: String list, mailing address.

    • Msg: Send Message

Notice here that the third parameter, MSG, is a string that represents the message. We know that the mail is generally composed of the title, sender, recipient, mail content, attachments, etc., when sending mail, pay attention to the format of MSG. This format is the format defined in the SMTP protocol.

The SMTP class also has the following methods:

Smtp.connect ([Host[,post]]) #连接远程smtp主机方法, the parameter is the remote host address, Port Smtp.login (User,password) #远程smtp主机的校验方法, Parameters are user name and password smtp.starttls ([keyfile[,certfile]]) #启用TLS (Secure transport) mode, all SMTP instructions will be encrypted for transmission. For example, when you use Gmail's SMTP service, you need to start this item to send the message normally smtp.quit () #断开SMTP服务器的连接


MIME, English is all called "Multipurpose Internet Mail Extensions", that is, multi-purpose Internet Mail extension, is the current Internet e-mail messages generally follow the technical specifications.

Python's commonly used MIME implementation classes:


Email.mime.multipart.MIMEMultipart ([_subtype[,boundary[,_subparts[,_params]])

#作用是生成包含多个部分的邮件体的MIME对象, the parameter _subtype specifies the optional three seed types to add to the "Content-type:multipart/subtype" header, respectively, mixed, related, Altermative, the default value is mixed.





Email.mime:audio. Mimeaudio (_audiodata[,_subtype[,_encoder[,**_params]])

#创建包含音频数据的邮件体, _audiodata A byte string containing the raw binary audio data.


Email.mime:image. Mimeimage (_imagedata[,_subtype[,_encoder[,**_params]])

#创建包含图片数据的邮件体, _imagedata A byte string containing the original picture-frequency data.


Email.mime.text.MIMEText (_text[,_subtype[,_charset])

#创建包含文本数据的邮件体, _text is a string that contains the message payload, _subtype specifies the text type, supports plain (default), or a string of HTML type

Simple common Mail instance:

#!/usr/bin/python# -*- coding: utf-8 -*-import smtplibfrom email.mime.text  import mimetextfrom email.header import header#  Third party  SMTP  service mail_host =   "smtp.**.com"   #  set up an SMTP server, such as:smtp.163.commail_user =  "****"    #  username mail_pass =  "* * *"   #  password sender =  ' **** ' receivers  =  ' * * * * @qq. com '   #  receive mail message = mimetext (' This is a python  test text ') message[' from '] = sendermessage[' to '] = receiverssubject =   ' one test mail ' message[' Subject '] = header (Subject) try:     Smtpobj = smtplib. SMTP ()     smtpobj.connect (mail_host, 25)   # 25  for  SMTP  Port number     smtpobj.login (Mail_user, mail_pass)     smtpobj.sendmail ( SenDer, receivers, message.as_string ())     print  ("Mail sent successfully") Except smtplib . smtpexception as e:    print  ("error:  Cannot send mail" +str (e))

This article is from the "Shei" blog, make sure to keep this source

Python Learning Notes-mail module SMTP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.