python 七種郵件內容發送方法實例

一、文件形式的郵件

[python]
#!/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′)#中文需參數‘utf-8’,單字節字符不需要
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()
[/python]

二、HTML形式的郵件

[python]?? ?
#!/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(‘</pre>
<h1>你好</h1>
<pre>’,’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()
[/python]

三、帶圖片的HTML郵件

[python]?? ?
#!/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.
<img alt="" src="cid:image1" />
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’, ”)
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
[/python]

四、帶附件的郵件

[python]? ?
#!/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’
#構造附件
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()
[/oython]

五、群郵件

[python]?? ?
#!/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(‘你好’,’text’,’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()
[/python]

六、各種元素都包含的郵件

[python]? ?
#!/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 = """\
Hi!
How are you?
Here is the <a href="http://www.python.org">link</a> you wanted.
"""
# 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)
#構造附件
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()
[/python]

七、基于SSL的郵件

[python]?? ?
#!/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′)#中文需參數‘utf-8’,單字節字符不需要
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()
[/python]

 

原創文章,作者:stanley,如若轉載,請注明出處:http://www.www58058.com/276

(0)
stanleystanley
上一篇 2015-01-16
下一篇 2015-02-07

相關推薦

  • python函數與作用域

    ##函數– 函數— 數學定義:y=f(x),y是x的函數,x是自變量— python函數:由若干語句組成的語句塊、函數名稱、參數列表構成,它是組織代碼的最小單元;完成一定的功能 – 函數的作用— 結構化編程對代碼的最基本的封裝,一般按照功能組織一段代碼— 封裝的目的是為了復用,減少冗余代…

    2017-10-17
  • python函數知識點,你掌握幾個?

    python函數的簡單介紹和用法

    2017-10-14
  • 遞歸函數

    遞歸函數 def foo(b,b1=3):print(“foo1 called “,b,b1)def foo2(c):foo3(c)print(“foo2 called”,c)def foo3(d):print(“foo3 called”)def mian():print(“…

    2018-04-16
  • Python基礎之if while for循環練習

    if for while循環練習 沒有邊界的最好用while,有邊界的最好用for 1.給定一個不超過5位數的正整數,判斷其有幾位 num = int(input()) if num<10: print(‘一位’) elif num<100: print(‘兩位’) elif num<1000: print(‘三位’) elif num&l…

    2017-09-16
  • Python面向對象基礎

    語言分類 面向機器 抽象成機器指令,讓機器容易理解 代表:匯編語言 面向過程 按照步驟一步一步走,若出現情況A做相應的處理,若出現情況B做相應的處理 問題規模小,可以步驟化,按部就班處理 代表:C 面向對象OOP 計算機需要處理的問題的規模越來越大,需要多人、多部門協作 代表:C++、Java、Python 面向對象 一種認識世界、分析世界的方法論。將萬事萬…

    2018-05-06
  • Python模塊及詳解(2)

    目錄: 一、報表之Excel操作XlsxWriter模塊 二、Python與rrdtool的結合模塊 三、構建集中式的病毒掃描機制 四、系統批量運維管理器paramiko詳解 五、系統批量運維管理器Fabric詳解 一、報表之Excel操作XlsxWriter模塊 Excel是當今最流行的電子表格處理軟件,支持豐富的計算函數及圖表,在系統運營方面廣泛用于運營…

    2018-01-17

評論列表(2條)

  • Gavin
    Gavin 2015-02-09 10:04

    實用,手工贊!

  • jie7832
    jie7832 2015-02-09 15:08

    很實用。。。謝謝樓主分享

欧美性久久久久