Python Send mail

Source: Internet
Author: User

The python2.3+ default comes with the Smtplib module, which can be used to emulate SMTP clients such as Foxmail for mail sending.

Common classes and methods for 1.smtplib modules

SMTP class Definition: SMTPLIB.SMTP ([Host[,port[,local_hostname[,timeout]]), as the constructor of SMTP, the function is to establish a connection with the SMTP server, after the connection is successful, Can send related requests to the server, such as login, check, send, exit, etc.

    • The host parameter is a remote SMTP host address, such as smtp.163.com.
    • The port parameter is the connection port, which defaults to 25.
    • The role of Local_hostname is to send the Helo/ehlo (Identity user identity) directive to the FQDN (full domain name) of the local host.
    • Timeout is the number of seconds to connect or try to time out.

The SMTP class has the following methods:

    • Smtp.connect ([Host[,port]]): The method of connecting to the remote SMTP host, host is the remote host address, Port is the remote host SMTP port, default is 25. It can also be expressed directly using the Host:port form, for example: Smtp.connect ("smtp.163.com", "25").
    • Smtp.login (User,password): The check method of the remote SMTP host, the parameter is the user name and password.
    • Smtp.sendmail (From_addr,to_addrs,msg[,mail_options,tcpt_options]): To implement the message sending function, the parameters are the sender, the recipient, the message content.
    • SMTP.STARTTLS ([Keyfile[,certfile]]): Enable TLS (Secure transport) mode, all SMTP directives will be encrypted for transmission, such as the need to start this when using Gmail's SMTP service to send mail normally. such as: Smtp.starttls ().
    • Smtp.quit (): Disconnect the SMTP server.

2. Example

This example uses the NetEase 163 mailbox to send a test mail to the QQ mailbox.

1 #!/usr/bin/python2 ImportSmtplib3 Importstring4 5HOST ="smtp.163.com"6SUBJECT ="Test email-2 from Python"7from ="[email protected]"8Password ="YourPassword"9to ="[email protected]"TenText ="Hello,this is a test Mail!\t\ri love python!" OneBODY =String.Join (( A     "From :%s"%From , -     "To :%s"%to, -     "Subject:%s"%SUBJECT, the     "", - text -),"\ r \ n") -Server =Smtplib. SMTP () +Server.connect (HOST," -") - Server.starttls () + Server.login (From,password) A Server.sendmail (from,[to],body) atServer.quit ()
sendmaildemo.py

The results are as follows:

Python Send mail

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.