欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python發(fā)送郵件示例(支持中文郵件標(biāo)題)

 更新時(shí)間:2014年02月16日 15:26:38   作者:  
python發(fā)送中文郵件示例,支持中文郵件標(biāo)題和中文郵件內(nèi)容。支持多附件。根據(jù)用戶名推測(cè)郵件服務(wù)器提供商

復(fù)制代碼 代碼如下:

def sendmail(login={},mail={}):
    '''\
    @param login login['user'] login['passwd']
    @param mail mail['to_addr'] mail['subject'] mail['content'] mail['attach']
    '''
    from datetime import datetime
    from base64 import b64encode
    import smtplib, mimetypes
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.mime.image import MIMEImage

    user_info = login['user'].split('@')
    mail_configure = {}
    mail_configure['mail_encoding'] = 'utf-8'
    mail_configure['mail_supplier'] = user_info[1]
    mail_configure['from_addr'] = login['user']
    mail_configure['server_host'] = 'smtp.%s' % mail_configure['mail_supplier']
    error = None

    try:
        email = MIMEMultipart()
        email['from'] = mail_configure['from_addr']
        email['to'] = mail['to_addr']
        email['subject'] = '=?%s?B?%s?=' % (mail_configure['mail_encoding'],b64encode(mail['subject']))
        email_content = MIMEText(mail['content'], _charset=mail_configure['mail_encoding'])
        email.attach(email_content)

        if 'attach' in mail:
            for i in mail['attach']:
                ctype, encoding = mimetypes.guess_type(i)
                if ctype is None or not encoding is None:
                    ctype = 'application/octet-stream'
                maintype, subtype = ctype.split('/', 1)
                att = MIMEImage((lambda f: (f.read(), f.close()))(open(i, 'rb'))[0], _subtype = subtype)
                att.add_header('Content-Disposition', 'attachment', filename = i)
                email.attach(att)

        smtp = smtplib.SMTP()
        smtp.connect(mail_configure['server_host'])
        smtp.login(user_info[0], login['passwd'])
        smtp.sendmail(mail_configure['from_addr'], mail['to_addr'], email.as_string())
        smtp.quit()
    except Exception as e:
        error = e

    return (mail_configure['from_addr'], mail['to_addr'], error)

測(cè)試

復(fù)制代碼 代碼如下:

def t21():
    login = {
        'user':'ak43@sina.com',
        'passwd':'hello@d'
    }
    mail = {
        'to_addr':'ak32@sina.com;ak32@21cn.com',
        'subject':'不帶附件的測(cè)試郵件',
        'content':'''\
        sz002718,友邦吊頂
        sz002719,麥趣爾
        sz002722,金輪股份
        ''',
    }
    print sendmail(login, mail)

    login = {
        'user':'hellot@sina.com',
        'passwd':'hello#world'
    }
    mail = {
        'to_addr':'tom12@sina.com;tom12@21cn.com',
        'subject':'帶附件的測(cè)試郵件',
        'content':'''\
        sz002718,友邦吊頂
        sz002719,麥趣爾
        sz002722,金輪股份
        ''',
        'attach':['e:/a/a.txt']
    }
    print sendmail(login, mail)

相關(guān)文章

  • Python 玩轉(zhuǎn)圖像格式轉(zhuǎn)換操作

    Python 玩轉(zhuǎn)圖像格式轉(zhuǎn)換操作

    這篇文章主要介紹了Python 玩轉(zhuǎn)圖像格式轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • anaconda中安裝的python環(huán)境中沒有pip3的問題及解決

    anaconda中安裝的python環(huán)境中沒有pip3的問題及解決

    這篇文章主要介紹了anaconda中安裝的python環(huán)境中沒有pip3的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 利用Python操作消息隊(duì)列RabbitMQ的方法教程

    利用Python操作消息隊(duì)列RabbitMQ的方法教程

    RabbitMQ是一個(gè)在AMQP基礎(chǔ)上完整的,可復(fù)用的企業(yè)消息系統(tǒng)。他遵循Mozilla Public License開源協(xié)議。下面這篇文章主要給大家介紹了關(guān)于利用Python操作消息隊(duì)列RabbitMQ的方法教程,需要的朋友可以參考下。
    2017-07-07
  • python實(shí)現(xiàn)循環(huán)語句1到100累和

    python實(shí)現(xiàn)循環(huán)語句1到100累和

    這篇文章主要介紹了python循環(huán)語句1到100累和方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 詳解python的幾種標(biāo)準(zhǔn)輸出重定向方式

    詳解python的幾種標(biāo)準(zhǔn)輸出重定向方式

    這篇文章是基于Python2.7版本,介紹常見的幾種標(biāo)準(zhǔn)輸出(stdout)重定向方式。顯然,這些方式也適用于標(biāo)準(zhǔn)錯(cuò)誤重定向。學(xué)習(xí)python的小伙伴們可以參考借鑒。
    2016-08-08
  • python小例子-縮進(jìn)式編碼+算術(shù)運(yùn)算符+定義與賦值

    python小例子-縮進(jìn)式編碼+算術(shù)運(yùn)算符+定義與賦值

    這篇文章主要給大家分享一些python學(xué)習(xí)小例子,內(nèi)容包括縮進(jìn)式編碼風(fēng)格、算術(shù)運(yùn)算符、定義與賦值,需要的小伙伴可以參考一下
    2022-04-04
  • ?Python錯(cuò)誤與異常處理

    ?Python錯(cuò)誤與異常處理

    這篇文章主要介紹了?Python錯(cuò)誤與異常處理,錯(cuò)誤與異常處理在Python中具有非常重要的地位,熟練的使用錯(cuò)誤與異常處理能夠?yàn)槲覀兊腜ython編程提供很多的便利之處,希望您閱讀完本文后能夠有所收獲
    2022-01-01
  • Python一些基本的圖像操作和處理總結(jié)

    Python一些基本的圖像操作和處理總結(jié)

    今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識(shí),文章圍繞著Python圖像操作和處理展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • python基礎(chǔ)教程之while循環(huán)

    python基礎(chǔ)教程之while循環(huán)

    這篇文章主要給大家介紹了關(guān)于python基礎(chǔ)教程之while循環(huán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • python flask 多對(duì)多表查詢功能

    python flask 多對(duì)多表查詢功能

    我們?cè)趂lask的學(xué)習(xí)中,會(huì)難免遇到多對(duì)多表的查詢,今天我也遇到了這個(gè)問題。下面把我的思路分享到腳本之家平臺(tái),供大家參考
    2017-06-06

最新評(píng)論