Python中phpMailer类的等价物在哪?
我刚开始学Python。
其实,我想用Python发送带有特色的邮件:包括HTML格式的内容、文本替代内容和附件。
我找到了一些教程,这个教程,然后我把它改成了可以用Gmail认证的版本(相关教程在这里)。
我现在的代码是这样的:
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 = "thegmailaccount@gmail.com"
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, "example@example.com", message)
server.close()
这个代码可以正常工作,但现在只缺少附件。
我还不知道怎么添加附件(我参考了这篇文章)。
那么,为什么Python没有像PHP的phpMailer那样的类呢?
是因为对于一个中等水平的Python程序员来说,发送带有HTML内容、附件和替代文本的邮件太简单了,所以不需要这样的类吗?还是说我只是没找到?
如果我能写出这样的类,当我对Python足够熟练的时候,这对别人会有用吗?
5 个回答
2
我想推荐一下 Lamson Project,这是我在找到这个讨论时正在寻找的东西。我做了一些额外的搜索,终于找到了它。它的目标是:
Lamson的目标是结束“电子邮件应用开发”的痛苦。与其停留在上世纪70年代,Lamson采用现代网页应用框架的设计,并使用一种经过验证的脚本语言(Python)。
它与Django的结合非常好。不过,它更适合基于电子邮件的应用。看起来真的很棒。
3
Django框架里有你需要的类,具体内容可以查看这份文档
from django.core.mail import EmailMultiAlternatives
subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
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 = 'name@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
6
如果你能原谅我稍微自我宣传一下,我写了一个邮件发送模块,用Python发送邮件变得相当简单。除了Python自带的smtplib和email库外,没有其他依赖。
这里有一个简单的例子,展示如何发送带附件的邮件:
from mailer import Mailer
from mailer import Message
message = Message(From="me@example.com",
To=["you@example.com", "him@example.com"])
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="me@example.com",
To="you@example.com",
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上更新了mailer的新版本,现在可以在Mailer类中指定端口了。