經過驗證的python發送郵件程式

來源:互聯網
上載者:User

#由於今年要開展郵件測試,所以研究一下郵件的東東,順便學習一下python,始終認為解釋性語言是自動化測試的利器,

但是由於一般指令碼語言都存在物件導向不易的缺陷,不容易在大的項目中使用,根據初步學習python具備良好的物件導向特性,

而且根據學習,發現這門語言的確相當好玩,很吸引我,特抄錄了一段代碼驗證了一下,測試通過,發帖如下。

import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib

def sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText):

        strFrom = fromAdd
        strTo = ', '.join(toAdd)

        server = authInfo.get('server')
        user = authInfo.get('user')
        passwd = authInfo.get('password')

        if not (server and user and passwd) :
                print 'incomplete login info, exit now'
                return

        # 設定root資訊
        msgRoot = email.MIMEMultipart.MIMEMultipart('related')
        msgRoot['Subject'] = subject
        msgRoot['From'] = strFrom
        msgRoot['To'] = strTo
        msgRoot.preamble = 'This is a multi-part message in MIME format.'

        # Encapsulate the plain and HTML versions of the message body in an
        # 'alternative' part, so message agents can decide which they want to display.
        msgAlternative = MIMEMultipart('alternative')
        msgRoot.attach(msgAlternative)

        #設定純文字資訊
#        msgText = MIMEText(plainText, 'plain', 'utf-8')
#        msgAlternative.attach(msgText)

        #設定HTML資訊
        msgText = email.MIMEText.MIMEText(htmlText, 'html', 'utf-8')
        msgAlternative.attach(msgText)

       #設定內建圖片資訊
#        fp = open('test.jpg', 'rb')
#        msgImage = MIMEImage(fp.read())
#        fp.close()
#        msgImage.add_header('Content-ID', '<image1>')
#        msgRoot.attach(msgImage)

       #發送郵件
        smtp = smtplib.SMTP()
       #設定調試層級,依情況而定
        smtp.set_debuglevel(1)
        smtp.connect(server)
        smtp.login(user, passwd)
        smtp.sendmail(strFrom, strTo, msgRoot.as_string())
#        smtp.sendmail(strFrom, strTo, msgRoot.as_string())
        smtp.quit()
        return

if __name__ == '__main__' :
        authInfo = {}
        authInfo['server'] = 'smtp.163.com'
        authInfo['user'] = '****@163.com'
        authInfo['password'] = '*****'//寫上你的密碼
        fromAdd = '****@163.com'
        toAdd = ['****@163.com','elbert.chenh@gamil.com']
        subject = 'hello ,boy title'
        plainText = '這裡是普通文本'
        htmlText = '<B>HTML文本</B>'
        sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.