欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python发送邮件(带附件)+屏幕截图

程序员文章站 2022-03-08 22:37:28
python发送邮件(带附件)+屏幕截图 import smtplib #smtplib这个模块是管发邮件 from email.mime.text import mimetext #构造...

python发送邮件(带附件)+屏幕截图

import smtplib
#smtplib这个模块是管发邮件
from email.mime.text import mimetext
#构造邮件内容
from email.mime.multipart import mimemultipart
#发带附件的邮件用的
email_host = 'smtp.163.com'     #邮箱服务器地址
email_user = 'xxx@163.com'  # 发送者账号
email_pwd = 'xxx'
# 发送者密码是邮箱的授权码,不是登录的密码
maillist = 'xxx@xxx.com'
#收件人邮箱,多个账号的话,用逗号隔开
new_msg = mimemultipart()
#构建了一个能发附件的邮件对象
new_msg.attach(mimetext('这是python测试发邮件的邮件,不要回复'))
# 邮件内容
new_msg['subject'] = 'python测试邮件带附件'    # 邮件主题
new_msg['from'] = email_user    # 发送者账号
new_msg['to'] = maillist    # 接收者账号列表
att = mimetext(open('like_report.txt').read())
att["content-type"] = 'application/octet-stream'
att["content-disposition"] = 'attachment; filename="haha.txt"'
new_msg.attach(att)
smtp = smtplib.smtp(email_host,port=25) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp.login(email_user, email_pwd)   # 发送者的邮箱账号,密码
smtp.sendmail(email_user, maillist, new_msg.as_string())
# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp.quit() # 发送完毕后退出smtp

已调试通过,以下是完整代码

 

# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import mimetext
from datetime import datetime
import time
from pil import imagegrab
from email.mime.application import mimeapplication
from email.mime.multipart import mimemultipart
import os


mailto_list=['panyu881024@163.com']           #收件人(列表)
huawei_to_list= ['panyu10@huawei.com']
mail_host="smtp.163.com"                      #使用的邮箱的smtp服务器地址,这里是163的smtp地址
mail_user="test_time_for_me@163.com"          #用户名
mail_pass=""                        #密码
mail_postfix="163.com"                        #邮箱的后缀,网易就是163.com


def send_mail(to_list,sub,content,pic_name):
    me="hello"+"<"+mail_user+"@"+mail_postfix+">"
    #msg = mimetext(content,_subtype='plain')
    msg = mimemultipart()
    msg['subject'] = sub
    msg['from'] = me
    msg['to'] = ";".join(to_list)                #将收件人列表以‘;’分隔
    puretext = mimetext('content: '+content)
    msg.attach(puretext)
    jpgpart = mimeapplication(open(pic_name, 'rb').read())
    jpgpart.add_header('content-disposition', 'attachment', filename=pic_name)
    msg.attach(jpgpart)
    try:
        server = smtplib.smtp()
        server.connect(mail_host)                            #连接服务器
        server.login(mail_user,mail_pass)               #登录操作
        server.sendmail(me, to_list, msg.as_string())
        server.close()
        return true
    except exception, e:
        print str(e)
        return false


while true:
    try:
        time_now = str(datetime.now())
        pic = imagegrab.grab()
        pic_name = time.strftime('%y-%m-%d-%h-%m-%s')+'.jpg'
        pic.save(pic_name)
        my_info = 'test_time_for_me'
        send_mail(huawei_to_list,time_now,my_info,pic_name)
        send_mail(mailto_list,time_now,my_info,pic_name)
        #os.remove(pic_name)
        print time_now
        time.sleep(600)
    except exception ,e:
        print e
        send_mail_bak()
        time.sleep(300)