发邮件

邮件发送,

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议。他是一组用于由源地址到目的地址传送邮件的规则,由它来控制新建的中转方式

 

 

 发送邮件的步骤

1.登录邮件服务器

2.构造符合邮件协议规则要求的邮件内容(email模块)

3.发送

发送一封邮件最简单的信语法如下

python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。

smtplib模块主要负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。

email模块主要负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。

# 发送HTML格式的邮件
import smtplib
from email.mime.text import MIMEText # 邮件正文
from email.header import Header # 邮件头
# 登录邮件服务器
smtp_obj = smtplib.SMTP_SSL("smtp.qq.com",465) # 发件人邮箱中的SMTP服务器,端口是25
smtp_obj.login("12***169@qq.com","bouddzvqmexgbage") # 发件人邮箱账号,邮箱密码
# smtp_obj.set_debuglevel(1) # 显示调试信息

# 设置邮件内容
mail_body = """
    <h5>hello,各位领导</h5>
    <p>
        Hello,这是第一封Python邮件
    </p>
"""    
msg = MIMEText(mail_body,"html","utf-8") # "plain"纯 文本格式
msg["From"]= Header("admin","utf-8") # 发送者
msg["To"] = Header("各位领导","utf-8") # 接收者
msg['Subject'] = Header("测试邮件","utf-8") # 主题

# 发送
smtp_obj.sendmail("129****9@qq.com",["te***g@sina.com","te**ng@163.com"],msg.as_string())

 

# 发送邮件的简单语法
import smtplib
from email.mime.text import MIMEText # 邮件正文
from email.header import Header # 邮件头
# 登录邮件服务器
smtp_obj = smtplib.SMTP_SSL("smtp.qq.com",465) # 发件人邮箱中的SMTP服务器,端口是25
smtp_obj.login("129***9@qq.com","bouddzvqmexgbage") # 发件人邮箱账号,邮箱密码
# smtp_obj.set_debuglevel(1) # 显示调试信息

# 设置邮件头信息
msg = MIMEText("hello,这是第一封Python邮件","plain","utf-8") # "plain"纯 文本格式
msg["From"]= Header("admin","utf-8") # 发送者
msg["To"] = Header("各位领导","utf-8") # 接收者
msg['Subject'] = Header("测试邮件","utf-8") # 主题

# 发送
smtp_obj.sendmail("129***69@qq.com",["te*ng@sina.com","te*g@163.com"],msg.as_string())

 

# 添加附件
sendfile=open(r'D:\pythontest\1111.txt','rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8')    
text_att["Content-Type"] = 'application/octet-stream'    
text_att["Content-Disposition"] = 'attachment; filename="显示的名字.txt"'  

 

# 添加图片
sendimagefile=open(r'D:\pythontest\testimage.png','rb').read()
image = MIMEImage(sendimagefile)
image.add_header('Content-ID','<image1>')

 

#coding: utf-8    
  
import smtplib    
from email.mime.multipart import MIMEMultipart    
from email.mime.text import MIMEText    
from email.mime.image import MIMEImage 
from email.header import Header   
    
#设置smtplib所需的参数
#下面的发件人,收件人是用于邮件传输的。
smtpserver = 'smtp.163.com'
username = 'XXX@163.com'
password='XXX'
sender='XXX@163.com'
#receiver='XXX@126.com'
#收件人为多个收件人
receiver=['XXX@126.com','XXX@126.com']

subject = 'Python email test'
#通过Header对象编码的文本,包含utf-8编码信息和Base64编码信息。以下中文名测试ok
#subject = '中文标题'
#subject=Header(subject, 'utf-8').encode()
    
#构造邮件对象MIMEMultipart对象
#下面的主题,发件人,收件人,日期是显示在邮件页面上的。
msg = MIMEMultipart('mixed') 
msg['Subject'] = subject
msg['From'] = 'XXX@163.com <XXX@163.com>'
#msg['To'] = 'XXX@126.com'
#收件人为多个收件人,通过join将列表转换为以;为间隔的字符串
msg['To'] = ";".join(receiver) 
#msg['Date']='2012-3-16'

#构造文字内容   
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.baidu.com"    
text_plain = MIMEText(text,'plain', 'utf-8')    
msg.attach(text_plain)    

#构造图片链接
sendimagefile=open(r'D:\pythontest\testimage.png','rb').read()
image = MIMEImage(sendimagefile)
image.add_header('Content-ID','<image1>')
image["Content-Disposition"] = 'attachment; filename="testimage.png"'
msg.attach(image)

#构造html
#发送正文中的图片:由于包含未被许可的信息,网易邮箱定义为垃圾邮件,报554 DT:SPM :<p><img src="cid:image1"></p>
html = """
<html>  
  <head></head>  
  <body>  
    <p>Hi!<br>  
       How are you?<br>  
       Here is the <a href="http://www.baidu.com">link</a> you wanted.<br> 
    </p> 
  </body>  
</html>  
"""    
text_html = MIMEText(html,'html', 'utf-8')
text_html["Content-Disposition"] = 'attachment; filename="texthtml.html"'   
msg.attach(text_html)    


#构造附件
sendfile=open(r'D:\pythontest\1111.txt','rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8') 
text_att["Content-Type"] = 'application/octet-stream'  
#以下附件可以重命名成aaa.txt  
#text_att["Content-Disposition"] = 'attachment; filename="aaa.txt"'
#另一种实现方式
text_att.add_header('Content-Disposition', 'attachment', filename='aaa.txt')
#以下中文测试不ok
#text_att["Content-Disposition"] = u'attachment; filename="中文附件.txt"'.decode('utf-8')
msg.attach(text_att)    
       
#发送邮件
smtp = smtplib.SMTP()    
smtp.connect('smtp.163.com')
#我们用set_debuglevel(1)就可以打印出和SMTP服务器交互的所有信息。
#smtp.set_debuglevel(1)  
smtp.login(username, password)    
smtp.sendmail(sender, receiver, msg.as_string())    
smtp.quit()

 

posted @ 2021-05-02 14:00  正在学Python  阅读(278)  评论(0编辑  收藏  举报