python发送邮件方法
1、普通文本邮件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
mail_user="xxxx@126.com" #发送邮件的邮箱
mail_pass="xxxxxxx" #密码,口令
mailto_list="xxxxx@qq.com" #接受邮件的邮箱
mail_host="smtp.126.com" #设置服务器 例:smtp.126.com strstr='你好' #内容
msg = MIMEText(strstr,'plain','utf-8') #邮件类型设置为plain
msg['Subject'] = "主题" #主题
msg['From'] = mail_user
msg['To'] = mailto_list
#邮件中文如果显示乱码,可以加上下面两句
msg["Accept-Language"]="zh-CN"
msg["Accept-Charset"]="ISO-8859-1,utf-8" server = smtplib.SMTP()
server.connect(mail_host) #连接smtp邮件服务器
server.login(mail_user,mail_pass) #登录
server.sendmail(mail_user, mailto_list, msg.as_string()) #发送
server.close() #关闭
2、HTML格式邮件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
mail_user="xxxxxxx@126.com" #发送邮件的邮箱
mail_pass="xxxxxx" #口令
mailto_list="xxxxxx@qq.com" #接收邮件的邮箱
to_list=[mailto_list,]
mail_host="smtp.126.com" #设置服务器 msg = MIMEMultipart()
msg['Subject'] = "主题" #主题
msg['From'] = mail_user
msg['To'] = mailto_list
#正文
#<img src="cid:image1">为图片显示位置
strstr="""
<html>
<head>正文</head>
<body>
<h1>Hello</h1>
<h2>你们好</h2>
</body>
</html>
"""
htm=MIMEText(strstr,'html','utf-8') #邮件类型设置为html
msg.attach(htm) server = smtplib.SMTP()
server.connect(mail_host) #连接smtp邮件服务器
server.login(mail_user,mail_pass) #登录
server.sendmail(mail_user, to_list, msg.as_string()) #发送
server.close() #关闭
3、带附件的邮件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
mail_user="xxxx@126.com" #发送邮件的邮箱
mail_pass="xxxxxxx" #口令
mailto_list="xxxxx@qq.com" #接收邮件的邮箱
to_list=[mailto_list,]
mail_host="smtp.126.com" #设置服务器 msg = MIMEMultipart()
msg['Subject'] = "主题" #主题
msg['From'] = mail_user
msg['To'] = mailto_list
#文字部分
strstr="Hello" #文字内容
att = MIMEText(strstr,'plain','utf-8')
msg.attach(att)
#附件
att = MIMEApplication(open('E:\\111.txt','rb').read()) #你要发送的附件地址
att.add_header('Content-Disposition', 'attachment', filename="222.txt") #filename可随意取名
msg.attach(att) server = smtplib.SMTP()
server.connect(mail_host) #连接smtp邮件服务器
server.login(mail_user,mail_pass) #登录
server.sendmail(mail_user, to_list, msg.as_string()) #发送
server.close() #关闭
4、正文显示图片的邮件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
mail_user="xxxxx@126.com" #发送邮件的邮箱
mail_pass="xxxxxx" #口令
mailto_list="xxxxxx@qq.com" #接收邮件的邮箱
to_list=[mailto_list,]
mail_host="smtp.126.com" #设置服务器 msg = MIMEMultipart()
msg['Subject'] = "主题" #主题
msg['From'] = mail_user
msg['To'] = mailto_list
#正文
#<img src="cid:image1">为图片显示位置
strstr="""
<html>
<head>正文图片</head>
<body>
<p>Hello<br>
你们好<br>
<br><img src="cid:image1"></br>
</p>
</body>
</html>
"""
htm=MIMEText(strstr,'html','utf-8')
msg.attach(htm) image = MIMEImage(open("F:\\111.jpg",'rb').read())
image.add_header('Content-ID','<image1>')
msg.attach(image) server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(mail_user,mailto_list,msg.as_string())
server.quit()
python发送邮件方法的更多相关文章
- python发送邮件方法总结
python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点. 一.相关模块介绍 发送邮件主要用到了smtplib和ema ...
- 解读Python发送邮件
解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Pyt ...
- python 发送邮件例子
想到用python发送邮件 主要是服务器 有时候会产生coredump文件 ,然后因为脚本重启原因,服务器coredump产生后会重启 但是没有主动通知开发人员 想了下可以写个脚本一旦产生cored ...
- 【转】【Python】Python发送邮件(常见四种邮件内容)
在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调 ...
- Python发送邮件(最全)
简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件. Python提供smtplib模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听 ...
- python 发送邮件 <QQ+腾讯企业邮箱>
一.使用QQ邮箱或者腾讯企业邮箱 python 发送邮件属于网络编程方向的,在工作中,我需要经常用邮件来检测我的程序运行状况.使用起来十分方便,这里我就用腾讯企业邮箱作为我的收发邮箱来使用. 使用py ...
- python接口自动化(三十二)--Python发送邮件(常见四种邮件内容)番外篇——上(详解)
简介 本篇文章与前边没有多大关联,就是对前边有关发邮件的总结和梳理.在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下, ...
- Python发送邮件以及对其封装
对Python发送邮件进行封装 Python发送邮件分为四步 连接到smtp服务器 登陆smtp服务器 准备邮件 发送邮件 导入所需要的包 import smtplib from email.mime ...
- Python发送邮件(常见四种邮件内容)
Python发送邮件(常见四种邮件内容) 转载 2017年03月03日 17:17:04 转自:http://lizhenliang.blog.51cto.com/7876557/1875330 ...
随机推荐
- uilmit 优化
#!/bin/bash sed -i "/^ulimit -SHn.*/d" /etc/rc.local echo "ulimit -SHn 102400" & ...
- include文件时尽量使用绝对路径
1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id'] 的速度是$row[id]的7倍. 3.echo 比 print 快,并且使用ech ...
- HSV色彩空间
HSV是把H(色相),S(饱和度),V(亮度)当做色值来定位颜色的空间.色相的取值范围是0~360度,用来表示颜色的类别.其中红色是0度,绿色是120度,蓝色是240度.饱和度的取值范围是0%~100 ...
- Unity学习疑问记录之隐藏与显示物体
Unity3D中隐藏与显示物体的一些操作 http://unity3d.9tech.cn/news/2013/0930/33019.html
- Rearrange a string so that all same characters become d distance away
Given a string and a positive integer d. Some characters may be repeated in the given string. Rearra ...
- 【iCore3 双核心板_FPGA】例程五:Signal Tapll实验——逻辑分析仪
实验指导书及代码包下载: http://pan.baidu.com/s/1bnNRfaB iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- what's the difference between dim as and dim as new?
what's the difference between dim as and dim as new? There is no difference with value types (Intege ...
- PHP中空字符串介绍0、null、empty和false之间的关系
PHP中空字符串介绍0.null.empty和false之间的关系 作者: 字体:[增加 减小] 类型:转载 时间:2012-09-25 用PHP开发那么久,PHP中空字符串.0.null.emp ...
- Java简单类——双向一对多映射
class Item { // 父栏目 private int iid ; private String name ; private String note ; private Subitem su ...
- XE5 ImageList的BUG?
今天做界面, 在imagelist里加载一个带有半透明通道的PNG图, 结果发现图片居然发暗, 如下: 原图: IDE里加载以后的图: 明显变暗...查询了源码, 无果 然后又用2010去测试, 发现 ...