send pics and attachments in mail

 

#!/bin/python

#encoding: utf-8

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.image import MIMEImage

import smtplib,time,os,sys,string

import os

import os.path

import time

# ---------------------------------------------------------------#

# Info: Please set mail_host mail_send_user

#       mail_send_pas mail_postfix at the begin

# ---------------------------------------------------------------#

mail_host="smtp.exmail.qq.com"

mail_user="test_report"

# mail_pass="this is not correct"

mail_pass="0WyEnj6rwnPmzT15"

mail_postfix="qq.com"

# ---------------------------------------------------------------#

# build_receiver

#       Make string to list , which split by ','

#       Input:

#          receiver          String of mail_send_to , split by ','

#          cc                   String of mail_cc_to , split by ','

# ---------------------------------------------------------------#

def build_receiver(receiver , cc):

    newList = []

    for in string.splitfields(receiver,","):

        if x not in newList:

            newList.append(x)

    for in string.splitfields(cc,","):

        if x not in newList:

            newList.append(x)

    return newList

     

# ---------------------------------------------------------------#

# send_mail

#     Input:

#        to_list                String of mail_send_to , split by ','

#        mailcc_list          String of mail_cc_to , split by ','

#        sub                    name of mail subject

#        content              text of the mail

#        attachmentlist    List of the attachment

#        pngpathList        List of the pngpath

# ---------------------------------------------------------------#

def send_mail(to_list , mailcc_list , sub , content , attachmentlist , pngpathList = None ):

    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"

    msg = MIMEMultipart('related')

    msg['Subject'] = sub

    msg['From'] = me

    msg['To'] = to_list

    if mailcc_list != None:

        msg['Cc'] = mailcc_list

     

    msg.attach(MIMEText(content , 'html' 'utf-8'))

    # 画图

    if pngpathList != None:

        i = 0

        for pngpath in pngpathList:

            i += 1

            fp = open( pngpath , 'rb')

            msgImage = MIMEImage(fp.read())

            fp.close()

            msgImage.add_header('Content-ID''image' + str(i))

            msg.attach(msgImage)

    #构造附件

    try:

        for attachment in attachmentlist:

            att = MIMEText(open(attachment, 'rb').read(), 'base64''utf-8')

            att["Content-Type"] = 'application/octet-stream'

            att["Content-Disposition"] = 'attachment; filename='+os.path.basename(attachment)+''

            msg.attach(att)

    except:

        pass   

    # 增加邮件重发机制

    for in range(3):

        try:

            conn = smtplib.SMTP(mail_host , 25)

            conn.login( mail_user + "@" + mail_postfix , mail_pass)

            print '[SendMailINFO ] ' 'Login in SMTP success ...'

            break

        except Exception, e:

            print '[SendMailINFO ] ' + str(e).encode('utf-8')+ 'Please wait login in smtp try again ...'

            time.sleep(5)

    try:

        if mailcc_list != None:

            conn.sendmail(me, build_receiver(to_list , mailcc_list), msg.as_string())

        else:

            conn.sendmail(me, to_list , msg.as_string())

        conn.close()

        return True

    except Exception, e:

        print str(e).encode('utf-8')

        return False

猜你喜欢

转载自blog.csdn.net/cahesi/article/details/81450393