python發送郵件

# -*- coding: UTF-8 -*-html


import uuidpost

import smtplib  性能

from email.Header import Header 測試

from email.mime.text import MIMETextui

from email.mime.multipart import MIMEMultipart server

from email.mime.image import MIMEImagehtm


mail_host="smtp.alibaba-inc.com"ip

mail_port=465utf-8

mail_tolist=['"=?UTF-8?B?6am+6L2m5a+86Iiq5rWL6K+V57uE?=" <xxxx@list.alibaba-inc.com>'] ci

mail_user="yanyang.lyy@alibaba-inc.com"

mail_pass="xxxxxxxxxx"

mail_from='"=?GBK?B?wfXR4NH0KMH6yeAp?=" <yanyang.lyy@alibaba-inc.com>'

mail_postfix="alibaba-inc.com"

mail_subject="性能測試報告"


content_resultlog="Result.log"

content_image1="TransactionsPerSecond.png"

content_image2="ResponseTimesOverTime.png"

content_image3="PerfMon.png"


def send():


    msg = MIMEMultipart('alternative')


    #msg["Accept-Language"]="zh-CN"

    #msg["Accept-Charset"]="ISO-8859-1,utf-8"

    msgid = uuid.uuid1()

    msg['Message-ID']=str(msgid)+"."+mail_user

    msg['Subject'] = Header(mail_subject, 'utf-8')

    msg['From'] = mail_from

    msg['To'] = ";".join(mail_tolist)  


    fp = open(content_resultlog, 'rb')

    line1 = fp.readline()

    line2 = fp.readline()

    html = '''

          <html>

              <head></head>

              <body>

                <br>%s</br>

                <br>%s</br>

                <br><img src="cid:image1"></br>

                <br><img src="cid:image2"></br>

                <br><img src="cid:image3"></br>

              </body>

          </html>

          '''%(line1, line2)

    fp.close()


    text = MIMEText(html,'html', 'utf-8')

    msg.attach(text)


    fp1 = open(content_image1, 'rb')

    image1 = MIMEImage(fp1.read())

    fp1.close()

    image1.add_header('Content-ID', '<image1>')

    msg.attach(image1)


    fp2 = open(content_image2, 'rb')

    image2 = MIMEImage(fp2.read())

    fp2.close()

    image2.add_header('Content-ID', '<image2>')

    msg.attach(image2)


    fp3 = open(content_image3, 'rb')

    image3 = MIMEImage(fp3.read())

    fp3.close()

    image3.add_header('Content-ID', '<image3>')

    msg.attach(image3)


    try:  

        server = smtplib.SMTP_SSL(mail_host, mail_port)

        server.login(mail_user,mail_pass)  

        server.sendmail(mail_user, mail_tolist, msg.as_string())  

        server.close()  

        return True  

    except Exception, e:  

        print str(e)

        return False  


if __name__ == '__main__':  

    if send():  

        print "發送成功"

    else:  

        print "發送失敗"  

相關文章
相關標籤/搜索