zoukankan      html  css  js  c++  java
  • Python发送邮件代码

    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', '0')
         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 )
  • 相关阅读:
    b_zj_最大点集(排序+小思维)
    b_zj_推箱子(记录人与箱子状态)
    b_zj_头条校招(分类讨论)
    Mybatis基础:缓存
    MyBatis嵌套查寻&嵌套结果查询--复杂查询
    MyBatis报错: java.lang.IllegalArgumentException: Parameter Maps collection does not contain value for com.wang.da
    log4j.properties详细配置 超干净!
    mybatis事务处理
    第一次使用MyBatis
    什么是MyBatis?它是用来做什么的?
  • 原文地址:https://www.cnblogs.com/Jims2016/p/7134710.html
Copyright © 2011-2022 走看看