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. DCloud-流应用:杂项

    ylbtech-DCloud-流应用:杂项 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回 ...

  2. java基础知识(14)---API

    API:(Application Programming Interface,编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力,而又无需访问源码,或理 ...

  3. 开发环境入门 linux基础 (部分)正则表达式 grep sed

    /etc/profile /etc/bashrc  .变量添加到shell环境中,永久生效. /root/.bashrc /root/.bash_profile 正则表达式 定义:正则就是用一些具有特 ...

  4. pycharm中 unittests in xxxx 运行模式

    pycham中 当你运行时 ,使用的 是  Run "unittests in  xxxx" 模式时候,if __name__ == '__main__':  后面的代码是不执行的 ...

  5. C Primer Plus学习笔记(六)- C 控制语句:分支和跳转

    if 语句: if 语句被称为分支语句(branching statement)或选择语句(selection statement) if 语句的通用形式: if (expression) state ...

  6. C#windows窗体应用程序如何自适应大小

    用C#的windows窗体应用程序做界面十分轻松,但是系统默认是没有让控件跟随窗体的大小改变而已改变的.所以需要我们手动去设置让窗体控件随着窗体的大小改变而改变.所以我们只需要将控件选择 然后把Anc ...

  7. 剑指offer 34_丑数

    丑数:只有2 3 5 这三个因子的数,求前(第)1500个.习惯上我们把1当作第一个丑数 例如 6, 8是丑数.14不是. #include <stdio.h> int Min(int x ...

  8. LinkedHashMap和HashMap的区别

    一.问题描述: 前几天写webservices接口,需要同步人力资源,涉及到添加顺序:主账号需要添加在次账号之前,直接上级需要添加在下级之前.解析xml之后直接封装在HashMap中,导致取对象时顺序 ...

  9. GUI编程02

    1 编写一个导航栏 from tkinter import * root = Tk() root.title("测试") root.geometry("400x400+4 ...

  10. JavaWeb面试题 有用

    ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获取数据,然后用JavaScript来操作DOM从而更新页面的局部显示. Ajax的优点: 1.最大的一点是页面 ...