function: send e-mail via mail server, Smtplib is an implementation of SMTP client, Support message format: text, HTML, Image, EXCEL
1. Ordinary text mail
#!/usr/bin/env python# coding:utf-8import smtplibimport stringhost = " Smtp.qq.com " #定义smtp主机subject = " Test email from python " #定义邮件主题to_mail = " [email protected] " #邮件收件人from_email = "[email protected]" #邮件发件人password = "Pass" # Message sender Mailbox Password text = "hello python!" #邮件内容body = string.join ( #组装sendmail方法的邮件主题内容, sections with "\ r \ n" for division "from: %s " % from_email, " to: %s " % to_ mail, "subject: %s" % subject, "", text ), "\ r \ n") server = smtplib. SMTP () #创建一个SMTP () object Server.connect (host, ") #通过connect方法连接smtp主机server. STARTTLS () # Start Secure transfer Mode Server.login (From_email,password) #邮箱账户登录认证server. SendMail ( From_email,to_mail,body) #邮件发送server. Quit () #断开smtp连接
This article is from the "Penguin" blog, please be sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1636514
Python Network Protocol Module Learning Smtplib