Python发送带附件的邮件代码

#coding: utf-8
import smtplib
import sys
import datetime
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart def send_mail(subject,mail_content,*receiver_all,**file_all): #发送带附件的邮件
#发送者:
sender = '764309404@qq.com' #接收着:
#receiver =['764309404@qq.com','1920583440@qq.com']
receiver = []
for rec in receiver_all:
receiver += rec
#用户名:
username = '764309404@qq.com' #密码:
password = 'ztoghcgoxraqbece' #邮件类型
msg = MIMEMultipart('alternative') #邮件主题:
#subject='' #定义邮件文本内容 for i in file_all:
file = str(file_all[i])
print file
#构造附件
att1 = MIMEText(open(file, 'rb').read(), 'base64', 'GBK')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename='+file
msg.attach(att1) #获取当前时间
dt = datetime.datetime.now() print dt #邮件主题供选择
text_morning = " 美好的一天从此刻开始,上班请记得打卡哦!......"
text_evening = " 辛勤的劳动结束咯,童鞋们签退自嗨啦!........." if int(dt.strftime('%H')) < 12:
part1 = MIMEText(mail_content,'plain','utf-8')
#subject = text_morning
else:
part1 = MIMEText(mail_content,'plain','utf-8')
#subject = text_evening msg['Subject'] = Header(subject, 'utf-8') msg.attach(part1) try:
smtp = smtplib.SMTP_SSL("smtp.qq.com",465)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print dt,'邮件发送成功'
except Exception,e:
print str(e)
print dt,'邮件发送发送失败' #send_mail('Hello','附件是今日爬取的天气和爬取日志',['764309404@qq.com','296738636@qq.com'],file1='/etl/etldata/script/ddl/mail/setup.py',file2='/etl/etldata/script/ddl/mail/mail.py')

python发送带网页和图片的代码

#coding: utf-8
import smtplib
import sys
import datetime
from email import encoders
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase #发送带附件的邮件
#sender = 'jiangjianming@hzyatop.com'
#sender = 'jim.jiang2016@outlook.com'
sender = '764309404@qq.com'
#receiver =['764309404@qq.com','1920583440@qq.com']
receiver =['764309404@qq.com','1920583440@qq.com','296738636@qq.com','328389557@qq.com','1507461103@qq.com','528857736@qq.com'] username = '764309404@qq.com'
password = 'ztoghcgoxraqbece'
msg = MIMEMultipart('alternative') subject='邮件主题' num = str(sys.argv[1]) with open('/etl/etldata/script/tmpdir/image/'+str(num)+'.jpg','rb') as f:
minme = MIMEBase('image','jpg',filename='1.jpg')
minme.add_header('Content-Disposition', 'attachment', filename=str(num)+'.png')
minme.add_header('Content-ID', '<0>')
minme.add_header('X-Attachment-Id', '')
minme.set_payload(f.read())
encoders.encode_base64(minme)
msg.attach(minme) dt = datetime.datetime.now() print dt def readcont(filepath):
f = open(filepath,'rb')
return f.read()
#return unicode(f.read(),'utf-8') #num=86128 subject = readcont('/etl/etldata/script/tmpdir/html/'+str(num)+'.title') note = readcont('/etl/etldata/script/tmpdir/html/'+str(num)+'.html') html_head = '<html></body><h1>' + subject + '</h1>'
html_end = '</body></html>'
html_pic = '<p><img src="cid:0"></p>'
html_note = '<p>' + note + '</p>'
html = html_head + html_pic + note + html_end msg.attach(MIMEText(html,'html','utf-8')) msg['Subject'] = Header(subject, 'utf-8')
#msg.attach(part1) try:
smtp = smtplib.SMTP_SSL("smtp.qq.com",465)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print '发送成功'
except Exception,e:
print str(e)
print '发送失败'

 发送带附件的邮件:

#coding: utf-8
#发生带附件的邮件代码
import smtplib
import sys
import datetime
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart def send_mail(subject,mail_content,*receiver_all,**file_all): #发送带附件的邮件
#发送者:
sender = '764309404@qq.com' #接收着:
#receiver =['764309404@qq.com','1920583440@qq.com']
receiver = []
for rec in receiver_all:
receiver += rec
#用户名:
username = '764309404@qq.com' #密码:
password = 'ztoghcgoxraqbece' #邮件类型
msg = MIMEMultipart('alternative') #邮件主题:
#subject=''
msg["From"] = '外星人'
#定义邮件文本内容 for i in file_all:
file = str(file_all[i])
print file
#构造附件
att = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename='+file
msg.attach(att) #获取当前时间
dt = datetime.datetime.now() print dt #邮件主题供选择
text_morning = " 美好的一天从此刻开始,上班请记得打卡哦!......"
text_evening = " 辛勤的劳动结束咯,童鞋们签退自嗨啦!........." if int(dt.strftime('%H')) < 12:
part1 = MIMEText(mail_content,'plain','utf-8')
#subject = text_morning
else:
part1 = MIMEText(mail_content,'plain','utf-8')
#subject = text_evening msg['Subject'] = Header(subject, 'utf-8') msg.attach(part1) try:
smtp = smtplib.SMTP_SSL("smtp.qq.com",465)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
print dt,'邮件发送成功'
except Exception,e:
print str(e)
print dt,'邮件发送发送失败' #date_hour_id = sys.argv[1] #date_id = date_hour_id[:8] #send_mail('兄弟,对不住了....%s汽车数据' %(date_id),'兄弟,对不住了,拿你邮箱来试试,你猜也猜不到我是谁,如果猜的不错的话,邮件会每隔四小时发一次,如果觉得扰民,那就等我明天上班来处理吧...附件是自动化爬取汽车之家的汽车数据,请查收.....',['764309404@qq.com','2586826993@qq.com'],file1='/etl/etldata/script/python/mb_qczj/%s/car_%s.csv' %(date_id,date_hour_id)) file = sys.argv[1] send_mail('汽车销量数据','附件是自动化爬取汽车销量排行数据,请查收.....',['764309404@qq.com','1920583440@qq.com'],file1 = file )

Python发送邮件代码的更多相关文章

  1. 使用python发送邮件

    最近需要做一个功能,统计bug的数量,然后发邮件给指定人,所有就先要了解一下使用python发送邮件 代码如下: #coding: utf-8 import smtplib from email.MI ...

  2. python发送邮件及附件

    今天给大伙说说python发送邮件,官方的多余的话自己去百度好了,还有一大堆文档说实话不到万不得已的时候一般人都不会去看,回归主题: 本人是mac如果没有按照依赖模块的请按照下面的截图安装 导入模块如 ...

  3. 解读Python发送邮件

    解读Python发送邮件 Python发送邮件需要smtplib和email两个模块.也正是由于我们在实际工作中可以导入这些模块,才使得处理工作中的任务变得更加的简单.今天,就来好好学习一下使用Pyt ...

  4. 利用python发送邮件

    找了很多使用python发送邮件的文章, 发现写的并不是太全, 导致坑特别多, 刚把这个坑跨过去, 在此记录下来 本代码使用163作为发送客户端, 接收邮箱随意 首先登录163邮箱, 开启POP3/S ...

  5. ETL过程跑完后,使用python发送邮件

    目标库中,如果有行数为0的表,使用python发送邮件 # -*- coding:utf-8 -*- # Author: zjc # Description:send monitor info to ...

  6. 【转】【Python】Python发送邮件(常见四种邮件内容)

    在写脚本时,放到后台运行,想知道执行情况,会通过邮件.SMS(短信).飞信.微信等方式通知管理员,用的最多的是邮件.在linux下,Shell脚本发送邮件告警是件很简单的事,有现成的邮件服务软件或者调 ...

  7. Python发送邮件(最全)

    简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件. Python提供smtplib模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听 ...

  8. (原创)python发送邮件

    这段时间一直在学习flask框架,看到flask扩展中有一个mail插件,所以今天就给大家演示如果发邮件. 首先我注册了一个163邮箱,需要开启smtp功能,因为咱们python发送邮件经过的是smt ...

  9. python发送邮件(转)

    SMTP发送邮件 阅读: 90274 SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和ema ...

随机推荐

  1. c# 设置水印,消除水印

    方案1: 图像处理 opencv etc 方案2: 开源框架,直接使用,已经优化 https://github.com/itext/itextsharp https://github.com/itex ...

  2. docker 安装mysql 使用navicat访问 解决

    1. 下载Mysql的Docker镜像: 2. 运行镜像,设置root账号初始密码(123456),映射本地宿主机端口3306到Docker端口3306.测试过程没有挂载本地数据盘: 3. 查看已运行 ...

  3. 12-01Js对象##正则表达式##

    正则表达式:是一个字符一个字符的验证,通过量词验证字符串: 1.什么是RegExp?RegExp是正则表达式的缩写.RegExp 对象用于规定在文本中检索的内容. 2.定义RegExp:var +变量 ...

  4. import javax.servlet 出错(真的很管用)

    Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServletRequest ...

  5. mahout in Action研读(1)-给用户推荐图书

    1.mahout in Action2.2第一个例子   Running a first recommender engine   数据: 第一个数字是用户ID 第二个是书的ID,第三个是用户对书的评 ...

  6. Windows系统上release版本程序bug跟踪解决方案-.dmp文件。

    使用场景: Win32程序在release模式下编译完成,发送给最终用户使用时,我们的程序有时候也会出现崩溃的情况,这个时候如果能快速定位崩溃原因或提供一些程序崩溃时的状态信息,对我们解决问题将会带来 ...

  7. SQL 连贯操作 [2]

    1.alias 用于设置数据表别名 $user = M('User'); var_dump($user->alias('anothername')->select()); 这时在SQL中的 ...

  8. 04 UUID

    1 什么是UUID UUID 的目的是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过中央控制端来做辨识资讯的指定. 2 应用场景 MySQL数据库不能想oracle数据库那样创建序列,就 ...

  9. Python 黑客 004 用Python构建一个SSH僵尸网络 01 简介

    用Python构建一个SSH僵尸网络 01 简介 一. 构建一个SSH僵尸网络的流程图: Created with Raphaël 2.1.0手动操作,实现通过SSH连接目标服务器(手动)用 Pexp ...

  10. Luogu 3193 [HNOI2008]GT考试

    BZOJ1009 妙! 推荐这篇题解: https://www.luogu.org/blog/Edgration/solution-p3193 考虑设计dp,设$f_{i, j}$表示长串匹配到i,短 ...