2009-04-30 53 views
3

我是新的蟒蛇.. 其實,我試圖發送精選的電子郵件與Python:HTML身體,文本替代身體和附件。Python的phpMailer php類在哪裏等同?

所以,我發現這個tutorial,使用Gmail認證適應它(教程中here

代碼中,我有個大氣壓,就是:

def createhtmlmail (html, text, subject): 
"""Create a mime-message that will render HTML in popular 
    MUAs, text in better ones""" 
import MimeWriter 
import mimetools 
import cStringIO 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEBase import MIMEBase 
from email.MIMEText import MIMEText 
from email.Utils import COMMASPACE, formatdate 
from email import Encoders 
import os 

out = cStringIO.StringIO() # output buffer for our message 
htmlin = cStringIO.StringIO(html) 
txtin = cStringIO.StringIO(text) 

writer = MimeWriter.MimeWriter(out) 
# 
# set up some basic headers... we put subject here 
# because smtplib.sendmail expects it to be in the 
# message body 
# 
writer.addheader("Subject", subject) 
writer.addheader("MIME-Version", "1.0") 
# 
# start the multipart section of the message 
# multipart/alternative seems to work better 
# on some MUAs than multipart/mixed 
# 
writer.startmultipartbody("alternative") 
writer.flushheaders() 
# 
# the plain text section 
# 
subpart = writer.nextpart() 
subpart.addheader("Content-Transfer-Encoding", "quoted-printable") 
pout = subpart.startbody("text/plain", [("charset", 'us-ascii')]) 
mimetools.encode(txtin, pout, 'quoted-printable') 
txtin.close() 
# 
# start the html subpart of the message 
# 
subpart = writer.nextpart() 
subpart.addheader("Content-Transfer-Encoding", "quoted-printable") 
# 
# returns us a file-ish object we can write to 
# 
pout = subpart.startbody("text/html", [("charset", 'us-ascii')]) 
mimetools.encode(htmlin, pout, 'quoted-printable') 
htmlin.close() 


# 
# Now that we're done, close our writer and 
# return the message body 
# 
writer.lastpart() 
msg = out.getvalue() 
out.close() 
return msg 

import smtplib 
f = open("/path/to/html/version.html", 'r') 
html = f.read() 
f.close() 
f = open("/path/to/txt/version.txt", 'r') 
text = f.read() 
subject = "Prova email html da python, con allegato!" 
message = createhtmlmail(html, text, subject) 
gmail_user = "[email protected]" 
gmail_pwd = "thegmailpassword" 
server = smtplib.SMTP("smtp.gmail.com", 587) 
server.ehlo() 
server.starttls() 
server.ehlo() 
server.login(gmail_user, gmail_pwd) 
server.sendmail(gmail_user, "[email protected]", message) 
server.close() 

和工作..現在只錯過附件.. 而我無法添加附件(從this後)

那麼,爲什麼沒有像phpMailer的PHP類的Python類? 這是因爲,對於一箇中等能力的Python程序員發送帶附件和ALT文本正文的HTML電子郵件是如此容易以至於不需要一個類? 還是因爲我只是沒有找到它?

如果我能寫一個這樣的類,當我用python寫得夠好的時候,會對某人有用嗎?

回答

5

如果你能原諒一些公然自我宣傳,我寫了一個mailer module,使發送電子郵件與Python相當簡單。除了Python smtplib和電子郵件庫以外,沒有其他依賴項。

下面是帶有附件發送電子郵件的簡單示例:

from mailer import Mailer 
from mailer import Message 

message = Message(From="[email protected]", 
        To=["[email protected]", "[email protected]"]) 
message.Subject = "Kitty with dynamite" 
message.Body = """Kitty go boom!""" 
message.attach("kitty.jpg") 

sender = Mailer('smtp.example.com') 
sender.login("username", "password") 
sender.send(message) 

編輯:這是用替代文本發送HTML電子郵件的一個例子。 :)

from mailer import Mailer 
from mailer import Message 

message = Message(From="[email protected]", 
        To="[email protected]", 
        charset="utf-8") 
message.Subject = "An HTML Email" 
message.Html = """This email uses <strong>HTML</strong>!""" 
message.Body = """This is alternate text.""" 

sender = Mailer('smtp.example.com') 
sender.send(message) 

編輯2:多虧了一個評論,我已經添加郵件的新版本的PyPI,讓你指定的梅勒類的端口。

+0

在示例中添加替代文字,你有一個勝利者! – YHVH 2009-04-30 18:32:18

0

我推薦閱讀SMTP rfc。谷歌搜索顯示,這可以很容易地通過使用您正在導入但從未使用的MimeMultipart類來完成。 Python的文檔網站上有some examples

+0

是的,我發誓我會讀一讀。 電子郵件類尚不存在的問題呢? 我會寫一個,隨地吐血,對某人有用,否則沒有人會使用它,因爲Python程序員'只是使用程序方法'? (我只是猜測啊) – Strae 2009-04-30 15:16:06

+0

你可能想使用像Django這樣的東西給你。 – 2009-04-30 15:27:43

2

Django包含你的核心需要的類,docs here

from django.core.mail import EmailMultiAlternatives 

subject, from_email, to = 'hello', '[email protected]', '[email protected]' 
text_content = 'This is an important message.' 
html_content = '<p>This is an <strong>important</strong> message.</p>' 
msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) 
msg.attach_alternative(html_content, "text/html") 
msg.attach_file('/path/to/file.jpg') 
msg.send() 

在我的設置,我有:

#GMAIL STUFF 
EMAIL_USE_TLS = True 
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_HOST_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = 'password' 
EMAIL_PORT = 587 
+0

這就是我一直在尋找的! 感謝隊友。 最後一件事:要使用該類,我們必須下載並安裝django,obvisiuly .. 冷是一個沒有django框架運行的類,但只是運行類文件(和自然的python模塊)) – Strae 2009-04-30 15:30:08

1

也許你可以用turbomail嘗試 python-turbomail.org

它更容易和有用的:)

import turbomail 

# ... 

message = turbomail.Message("[email protected]", "[email protected]", subject) 
message.plain = "Hello world!" 

turbomail.enqueue(message) 
2

只是想指出Lamson Project這就是我一直在尋找,當我找到了這個線程。我做了一些更多的搜索並找到它。它是:

Lamson's goal is to put an end to the hell that is "e-mail application development". Rather than stay stuck in the 1970s, Lamson adopts modern web application framework design and uses a proven scripting language (Python).

它很好地與Django的集成。但它更適用於基於電子郵件的應用程序。雖然它看起來像純粹的愛。