透過python發郵件

發表於2009-05-26
透過python發郵件[@more@]


email模組提供了把body組織成二進位制流的方法,配合 smtplib模組使用可以實現傳送附件(attachment)等功能。email模組把所有的內容包括正文、附件、時間、From、To、 Subject作為郵件的各個部分物件attach到郵件物件的根容器中,然後把容器轉成成字串(上例的body),然後透過 smtp.sendmail傳送。
# -*- coding: gbk -*-

import smtplib
import email.MIMEMultipart
import email.MIMEText
import email.MIMEBase
import os.path

def sendmail(from_addr, to_addr, subject, content, attachfilelist):
"""
attachfilelist use ;
"""
server = smtplib.SMTP("proxy-in.xxxx.com")
#server.login("username","password");

# 構造MIMEMultipart物件做為根容器
main_msg = email.MIMEMultipart.MIMEMultipart()

# 構造MIMEText物件做為郵件顯示內容並附加到根容器
text_msg = email.MIMEText.MIMEText(content)
main_msg.attach(text_msg)

# 構造MIMEBase物件做為檔案附件內容並附加到根容器
contype = 'application/octet-stream'
maintype, subtype = contype.split('/', 1)

# 讀入檔案內容並格式化
file_list=attachfilelist.split(';')
if (len(attachfilelist)>0 and len(file_list)>0):
for file_name in attachfilelist.split(';'):
if(len(file_name)>1):
data = open(file_name, 'rb')
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
file_msg.set_payload(data.read( ))
data.close( )
email.Encoders.encode_base64(file_msg)

## 設定附件頭
basename = os.path.basename(file_name)
file_msg.add_header('Content-Disposition', 'attachment', filename = file_name)
main_msg.attach(file_msg)

# 設定根容器屬性
main_msg['From'] = from_addr
main_msg['To'] = to_addr
main_msg['Subject'] = subject
main_msg['Date'] = email.Utils.formatdate( )

# 得到格式化後的完整文字
fullText = main_msg.as_string()
# 用smtp傳送郵件
to_list=[]
for addr in to_addr.split(';'):
to_list.append(addr)
try:
server.sendmail(main_msg['From'], tuple(to_list), fullText)
finally:
server.quit()

相關連結:%5Fsky/blog/item/df3b5fdf85607f154854030f.html
上面的連結從郵件的格式的角度出發分析email的原理,可以加深對傳送和接收郵件的理解

相關連結:_sky/blog/item/22ae71cf10042c3bf9dc6139.html
上面的連結給出了MIMEMultipartMIMEImageMIMEText的帶引數的用法,可以在正文裡面用html格式帶圖片,有意思。

相關連結:http://hi.baidu.com/python23/blog/item/a7a918d5cc5e0ec551da4beb.html
http://hi.baidu.com/python23/blog/item/42a5194c87160cf8d72afc1d.html
上面的連結從編解碼的角度去看email模組,也挺有意思的,解釋得比較透徹

相關連結:



================================

===================================

PYTHON 3.1.1 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:"Times New Roman";} HTML格式的,帶有中文的郵件。


/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif"; mso-fareast-font-family:"Times New Roman";}

# -*- encoding:utf-8 -*-

import smtplib

import email.message

import mimetypes

from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.header import Header

sender = ""

to = ""

subject = Header('這是一封測試郵件','utf-8')

cc = ""

htmlText2 = '

相關文章