Python 发送邮件案例
文件形式的邮件
#!/usr/bin/env python
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header sender = '***'
receiver = '***'
subject = 'test'
smtpserver = 'xxx'
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 python
#coding: utf-8
import smtplib
from email.mime.text import MIMEText sender = '***'
receiver = '***'
subject = 'test'
smtpserver = 'xxx'
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 python
#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 = 'test'
smtpserver = 'xxx'
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 python
#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 = 'test'
smtpserver = 'xxx'
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 python
#coding: utf-8
import smtplib
from email.mime.text import MIMEText sender = '***'
receiver = ['***','****',……]
subject = 'test'
smtpserver = 'xxx'
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 = 'test'
smtpserver = 'xxx'
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">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 python
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = '***'
receiver = '***'
subject = 'test'
smtpserver = 'xxx'
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 发送邮件案例的更多相关文章
- 最全总结!聊聊 Python 发送邮件的几种方式
1. 前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大 ...
- python发送邮件
python发送邮件(无附件) ======================================================= #!/usr/bin/env python#coding ...
- python发送邮件及附件
今天给大伙说说python发送邮件,官方的多余的话自己去百度好了,还有一大堆文档说实话不到万不得已的时候一般人都不会去看,回归主题: 本人是mac如果没有按照依赖模块的请按照下面的截图安装 导入模块如 ...
- python 发送邮件实例
留言板回复作者邮件提醒 -----------2016-5-11 15:03:58-- source:python发送邮件实例
- 解读Python发送邮件
解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Pyt ...
- python 发送邮件例子
想到用python发送邮件 主要是服务器 有时候会产生coredump文件 ,然后因为脚本重启原因,服务器coredump产生后会重启 但是没有主动通知开发人员 想了下可以写个脚本一旦产生cored ...
- 利用python发送邮件
找了很多使用python发送邮件的文章, 发现写的并不是太全, 导致坑特别多, 刚把这个坑跨过去, 在此记录下来 本代码使用163作为发送客户端, 接收邮箱随意 首先登录163邮箱, 开启POP3/S ...
- 用Python发送邮件
文件:send.py # -*- coding:utf-8 -*- # ## 任兴测试用Python发送邮件 import os import sys import getopt import tim ...
- ETL过程跑完后,使用python发送邮件
目标库中,如果有行数为0的表,使用python发送邮件 # -*- coding:utf-8 -*- # Author: zjc # Description:send monitor info to ...
随机推荐
- Openerp 修改 tree view 的列宽
在 tree 的后边添加自定义css 列:“my_class" 然后在对应的css文件中,添加样式: 保存,重新刷新页面即可.
- DB2 命令大全
check Archiving processing 查看日志归档情况 db2 "SELECT DATE(CAST(START_TIME as TIMESTAMP)) as DATE, co ...
- (转)OpenStack —— 原理架构介绍(一、二)
原文:http://blog.51cto.com/wzlinux/1961337 http://blog.51cto.com/wzlinux/category18.html-------------O ...
- unity 优化之overdraw查看
scene视图里面能直接看,打开左上角那个下拉选项,选择overdraw就行 转载篇在game视图下查看overdraw: https://blog.csdn.net/complicate ...
- tar.gz和bin,以及rpm,deb等linux后缀的文件的区别
先说tar.gz源码类的吧.这种要你手动安装,编译,首先你还要安装了相关的编译软件如gcc g++ 等.一般操作 configure , make && make install. 其 ...
- Mysql的with rollup分组统计功能(5.1以上版本)
RollUp是上卷功能,类似于数据挖掘中的上卷操作. ROLLUp的功能和Order by功能是互斥的. mysql> SELECT year, SUM(profit) FROM sales G ...
- Rsyslog+ELK日志分析系统
转自:https://www.cnblogs.com/itworks/p/7272740.html Rsyslog+ELK日志分析系统搭建总结1.0(测试环境) 因为工作需求,最近在搭建日志分析系统, ...
- Vue 多路由文件的合并
Vue 多路由文件的合并 1.使用的是ES6 数组的合并方法 let routes = new Set([...routes1, ...homerouters]);2.两个路由文件,导出的实际上就是一 ...
- [linux] shell脚本编程-ubuntu创建vsftpd服务
1. useradd -s /bin/bash -m 用户名 , 创建用户,自动创建家目录 , 设置登录shell 2. echo 用户名:密码 | chpasswd ,非交互式设置密码 3. ...
- 01 使用JavaScript原生控制div属性
写在前面: 因对前端开发感兴趣,于是自学前端技术,现在已经会HTML.CSS.JavaScript基础技术.但水平处于小白阶段,在网上找一些小项目练练手,促进自己的技术成长.文章记录自己的所思所想,以 ...