Python Learning note e-mail module smtplib

Source: Internet
Author: User

In the field of system management, we often use e-mail to send management information, business quality statements, etc., to facilitate the first time operators to understand the service status of the business. Through the Python smtplib module to implement the message sending function, simulates an SMTP client, through interacts with the SMTP server to implement the mail sends the function.

Common classes and methods of Smtplib modules

    • Smtp.connect ([Host[,port]) method, connect the remote SMTP host method, host is the remote host address, Port is the remote host SMTP port, the default is 25, can also be directly used in the form of Host:port to express, Example: Smtp.connect ("smtp.qq.com", "25")

    • Smtp.login (user, password) method, the remote host's check method.

    • Smtp.sendmail (from_addr, To_addrs, MSG, mail_options=[],rcpt_options=[]) method, to implement the message sending function, the parameters of the sender, the recipient, the message content, For example: Smtp.sendmail ("[Email protected]", "[email protected]", body), where the body content is as follows:

    • The Smtp.starttls () method enables TLS (secure transport) mode, and all SMTP instructions are encrypted for transmission.

    • Smtp.quit () method to disconnect from the SMTP server.


Note: The QQ mailbox needs to generate authorization code.

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/9A/FA/wKiom1lcuVewxW5DAAIjEDZrGLQ934.png-wh_500x0-wm_ 3-wmp_4-s_3317030553.png "title=" 2f3b1af728c52d46869299e22de23c16.png "alt=" Wkiom1lcuvewxw5daaijedzrglq934.png-wh_50 "/>


Example:

#!/usr/bin/env pythonimport smtplibhost = ' smtp.qq.com ' SUBJECT = ' Test email from python ' to = ' [email protected] ' from = ' [ Email protected] "test =" Just a test txt! " BODY = "\ r \ n". Join (("From:%s"%from, "to:%s"%to, "Subject:%s"%subject, "", Test)) Server = Smtplib. SMTP () Server.connect (HOST, "Server.starttls") server.login ("[Email protected]", "password") Server.sendmail ( From,[to],body) Server.quit ()



Python Learning note e-mail module smtplib

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.