Python-how to send mail via Gmail: Use of smtplib

Source: Internet
Author: User
Tags starttls how to send mail

What about SMTP? Encyclopedia business cards

SMTP (Simple Mail Transfer Protocol) is a Simple Mail Transfer Protocol. It is a set of rules used to send mail from the source address to the destination address, which controls the transfer mode of mail. The SMTP protocol belongs to the TCP/IP protocol family, which helps each computer locate the next destination when sending or transferring letters. The server specified by the SMTP protocol can send email to the recipient's server. The entire process takes several minutes. The SMTP server is a mail sending server that follows the SMTP protocol and is used to send or transmit emails.

SMTP is similar to HTTP and is in the text format at the application layer. As mentioned in the art of Unix programming, We need to advocate plain text protocols so that it is easy to analyze from the naked eye, so that we do not need to compile the corresponding visual editor for debugging.

However, when using Gmail, Wireshark analysis is not available except for the original handshake protocol (for example)

1. Establish a TCP three-way handshake

2. SMTP handshake

3. Start the SSL handshake on the client-Wait for the handshake on the server-start the SSL handshake

4. From here on, all content is encrypted.


So we have to enable the debug mode of SMTP: SMTP. set_debuglevel (1 ).

In this way, the interaction process can be printed in detail on the console:

All interactions start with 'send: ***** 'on the client, and the server returns 'reply: *****'. Is it similar to HTTP.


1. Connect smtp.gmail.com: 25, then the server returns the server information, and requires that SSL: 'starttls 'be enabled'

2. when TLS is enabled, Gmail requires authorization to continue the operation: auth login plain xoauto. Of course, all content after TLS is enabled cannot be captured by packet capture tools such as Wireshark, therefore, you don't have to worry about third parties intercepting this part of the content. That is why the webpage advocates HTTPS and cannot be intercepted by a third party.

3. authorization request-the authorization is successful.

4. After the authorization is successful, you can start to send the content:

5. Remember to exit ..

Whenever I look at the SMTP processing logic re-written by many people around the world, I want to tell him: Python already has a related library. Why not use it to recreate the wheel? Is it necessary to learn the essence of this protocol? In this case, I suggest you read HTTP. Now many network applications rely on HTTP. It is not advisable to study SMTP or recreate the wheel.

What if I don't know python?

You can understand the following code.

import smtplibimport sysimport email.mime.text# my test mailmail_username='harkyes@gmail.com'mail_password='harktest'from_addr = mail_usernameto_addrs=('lihe21327@gmail.com')# HOST & PORTHOST = 'smtp.gmail.com'PORT = 25# Create SMTP Objectsmtp = smtplib.SMTP()print 'connecting ...'# show the debug logsmtp.set_debuglevel(1)# connettry:    print smtp.connect(HOST,PORT)except:    print 'CONNECT ERROR ****'# gmail uses sslsmtp.starttls()# login with username & passwordtry:    print 'loginning ...'    smtp.login(mail_username,mail_password)except:    print 'LOGIN ERROR ****'# fill content with MIMEText's object msg = email.mime.text.MIMEText('Hi ,I am leehark')msg['From'] = from_addrmsg['To'] = ';'.join(to_addrs)msg['Subject']='hello , today is a special day'print msg.as_string()smtp.sendmail(from_addr,to_addrs,msg.as_string())smtp.quit()

Related Article

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: info-contact@alibabacloud.com 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.