python邮件发送smtplib使用详解

 更新时间:2020年06月16日 14:59:40   作者:johnny  
这篇文章主要为大家详细介绍了python邮件发送smtplib的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python邮件发送smtplib使用具体代码,供大家参考,具体内容如下

文件形式的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

HTML形式的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

带图片的HTML邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8') 
msgRoot.attach(msgText) 
 
fp = open('h:\\python\\1.jpg', 'rb') 
msgImage = MIMEImage(fp.read()) 
fp.close() 
 
msgImage.add_header('Content-ID', '<image1>') 
msgRoot.attach(msgImage) 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit() 

带附件的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
#构造附件 
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="1.jpg"' 
msgRoot.attach(att) 
  
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit() 

群发邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
 
sender = '***' 
receiver = ['***','****',……] 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

各种元素都包含的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
# Create message container - the correct MIME type is multipart/alternative. 
msg = MIMEMultipart('alternative') 
msg['Subject'] = "Link" 
 
# Create the body of the message (a plain-text and an HTML version). 
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org" 
html = """\ 
<html> 
 <head></head> 
 <body> 
 <p>Hi!<br> 
 How are you?<br> 
 Here is the <a href="http://www.python.org" rel="external nofollow" >link</a> you wanted. 
 </p> 
 </body> 
</html> 
""" 
 
# Record the MIME types of both parts - text/plain and text/html. 
part1 = MIMEText(text, 'plain') 
part2 = MIMEText(html, 'html') 
 
# Attach parts into message container. 
# According to RFC 2046, the last part of a multipart message, in this case 
# the HTML message, is best and preferred. 
msg.attach(part1) 
msg.attach(part2) 
#构造附件 
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="1.jpg"' 
msg.attach(att) 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

基于SSL的邮件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.ehlo() 
smtp.starttls() 
smtp.ehlo() 
smtp.set_debuglevel(1) 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】

    Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】

    这篇文章主要介绍了Python tensorflow实现mnist手写数字识别,结合实例形式分析了基于tensorflow模块使用非卷积与卷积算法实现手写数字识别的具体操作技巧,需要的朋友可以参考下
    2019-12-12
  • Python基于pygame模块播放MP3的方法示例

    Python基于pygame模块播放MP3的方法示例

    这篇文章主要介绍了Python基于pygame模块播放MP3的方法,结合实例形式简单分析了Python使用pygame模块播放MP3音频文件的实现方法以及相关函数的功能说明,需要的朋友可以参考下
    2017-09-09
  • 用python制作词云视频详解

    用python制作词云视频详解

    这篇文章主要介绍了用python制作词云视频详解,原理解释清晰,代码详细,用于练习很适合,需要的朋友可以参考下
    2021-04-04
  • Python数据分析之绘制ppi-cpi剪刀差图形

    Python数据分析之绘制ppi-cpi剪刀差图形

    这篇文章主要介绍了Python数据分析之绘制ppi-cpi剪刀差图形,ppi-cp剪刀差是通过这个指标可以了解当前的经济运行状况,下文更多详细内容介绍需要的小伙伴可以参考一下
    2022-05-05
  • python GUI库图形界面开发之pyinstaller打包python程序为exe安装文件

    python GUI库图形界面开发之pyinstaller打包python程序为exe安装文件

    这篇文章主要介绍了python GUI库图形界面开发之pyinstaller打包python程序为exe安装文件,需要的朋友可以参考下
    2020-02-02
  • python微信跳一跳系列之自动计算跳一跳距离

    python微信跳一跳系列之自动计算跳一跳距离

    这篇文章主要为大家详细介绍了python微信跳一跳系列之自动计算跳一跳距离,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • Python中的单例模式与反射机制详解

    Python中的单例模式与反射机制详解

    这篇文章主要为大家介绍了Python中的单例模式与反射机制,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-11-11
  • 浅谈Python3中print函数的换行

    浅谈Python3中print函数的换行

    这篇文章主要介绍了浅谈Python3中print函数的换行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • python实现广度优先搜索过程解析

    python实现广度优先搜索过程解析

    这篇文章主要介绍了python实现广度优先搜索过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Python三级菜单的实例

    Python三级菜单的实例

    下面小编就为大家带来一篇Python三级菜单的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09

最新评论