最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python發(fā)送郵件的實例代碼(支持html、圖片、附件)

 更新時間:2013年03月04日 23:12:43   作者:  
python發(fā)送郵件的一些例子,有需要的朋友可以參考下

第一段代碼:

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

#!/usr/bin/python
# -*- coding: utf-8 -*-

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

        # 設(shè)定root信息
        msgRoot = 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)

        #設(shè)定純文本信息
        msgText = MIMEText(plainText, 'plain', 'utf-8')
        msgAlternative.attach(msgText)

        #設(shè)定HTML信息
        msgText = MIMEText(htmlText, 'html', 'utf-8')
        msgAlternative.attach(msgText)

       #設(shè)定內(nèi)置圖片信息
        fp = open('test.jpg', 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', '<image1>')
        msgRoot.attach(msgImage)

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

if __name__ == '__main__' :
        authInfo = {}
        authInfo['server'] = 'smtp.somehost.com'
        authInfo['user'] = 'username'
        authInfo['password'] = 'password'
        fromAdd = 'username@somehost.com'
        toAdd = ['someone@somehost.com', 'other@somehost.com']
        subject = '郵件主題'
        plainText = '這里是普通文本'
        htmlText = '<B>HTML文本</B>'
        sendEmail(authInfo, fromAdd, toAdd, subject, plainText, htmlText)



文件形式的郵件

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

#!/usr/bin/env python3  
#coding: utf-8  
import smtplib  
from email.mime.text import MIMEText  
from email.header import Header  

sender = '***'  
receiver = '***'  
subject = 'python email test'  
smtpserver = 'smtp.163.com'  
username = '***'  
password = '***'  

msg = MIMEText('你好','text','utf-8')#中文需參數(shù)‘utf-8',單字節(jié)字符不需要  
msg['Subject'] = Header(subject, 'utf-8')  

smtp = smtplib.SMTP()  
smtp.connect('smtp.163.com')  
smtp.login(username, password)  
smtp.sendmail(sender, receiver, msg.as_string())  
smtp.quit()  

HTML形式的郵件

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

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')

msg['Subject'] = subject

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

帶圖片的HTML郵件

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

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'

msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')
msgRoot.attach(msgText)

fp = open('h:\\python\\1.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

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

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

帶附件的郵件

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

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'

#構(gòu)造附件
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msgRoot.attach(att)

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

群郵件

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

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText

sender = '***'
receiver = ['***','****',……]
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = MIMEText('你好','plain','utf-8')

msg['Subject'] = subject

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

各種元素都包含的郵件

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

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a >link</a> you wanted.
    </p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#構(gòu)造附件
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msg.attach(att)

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

基于SSL的郵件

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

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = MIMEText('你好','plain','utf-8')#中文需參數(shù)‘utf-8',單字節(jié)字符不需要
msg['Subject'] = Header(subject, 'utf-8')

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

相關(guān)文章

  • python計算RPKM操作示例詳解

    python計算RPKM操作示例詳解

    這篇文章主要為大家介紹了python計算RPKM操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • 解決Keras 與 Tensorflow 版本之間的兼容性問題

    解決Keras 與 Tensorflow 版本之間的兼容性問題

    今天小編就為大家分享一篇解決Keras 與 Tensorflow 版本之間的兼容性問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python實現(xiàn)五子棋算法

    python實現(xiàn)五子棋算法

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)五子棋算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Python生成8位隨機字符串的方法分析

    Python生成8位隨機字符串的方法分析

    這篇文章主要介紹了Python生成8位隨機字符串的方法,結(jié)合實例形式對比分析了2種比較常用的隨機字符串生成技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2017-12-12
  • python requests完成接口文件上傳的案例

    python requests完成接口文件上傳的案例

    這篇文章主要介紹了python requests完成接口文件上傳的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 使用Python進行穩(wěn)定可靠的文件操作詳解

    使用Python進行穩(wěn)定可靠的文件操作詳解

    在本文中,主要分享一些如何在Python代碼中改善I/O可靠性的見解,大家參考使用吧
    2013-12-12
  • 在主機商的共享服務(wù)器上部署Django站點的方法

    在主機商的共享服務(wù)器上部署Django站點的方法

    這篇文章主要介紹了在主機商的共享服務(wù)器上部署Django站點的方法,Django是最具人氣的Python框架,需要的朋友可以參考下
    2015-07-07
  • 教你使用Python獲取QQ音樂某個歌手的歌單

    教你使用Python獲取QQ音樂某個歌手的歌單

    這篇文章主要介紹了Python獲取QQ音樂某個歌手的歌單,從qq音樂中獲取某個你喜歡的歌手的清單,涉及到的庫有requests、json,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 使用Python AIML搭建聊天機器人的方法示例

    使用Python AIML搭建聊天機器人的方法示例

    這篇文章主要介紹了使用Python AIML搭建聊天機器人的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 在pycharm中調(diào)試fastapi應(yīng)用程序的流程步驟

    在pycharm中調(diào)試fastapi應(yīng)用程序的流程步驟

    ? FastAPI 是一個現(xiàn)代、快速(高性能)的 Web 框架,用于構(gòu)建基于 Python 的 API,它具有簡單易用的特性,同時也提供了高度自動化的文檔生成功能,本文給大家介紹了在pycharm中調(diào)試fastapi應(yīng)用程序的流程步驟,需要的朋友可以參考下
    2024-12-12

最新評論

深水埗区| 当阳市| 策勒县| 体育| 漠河县| 石城县| 乐平市| 湖州市| 莱芜市| 鸡泽县| 亚东县| 监利县| 武鸣县| 苏尼特右旗| 休宁县| 尚义县| 湖南省| 鄂托克前旗| 开鲁县| 余姚市| 石家庄市| 普兰店市| 三穗县| 济南市| 盐源县| 尤溪县| 会宁县| 观塘区| 鸡东县| 吴川市| 扎囊县| 望都县| 奉化市| 浠水县| 镇远县| 阿拉尔市| 兴化市| 汨罗市| 清镇市| 乌鲁木齐县| 张家港市|