3

我是 python 新手。实际上,我正在尝试使用 python 发送特色电子邮件:html 正文、文本替代正文和附件。

因此,我找到了本教程并使用 gmail 身份验证对其进行了修改(教程在此处找到)

我有atm的代码是:

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()

并且有效..现在只错过附件..而且我无法添加附件(来自这篇文章)

那么,为什么 php 没有像 phpMailer 这样的 python 类呢?是不是因为,对于一个中等能力的 Python 程序员来说,发送带有附件和替代文本正文的 html 电子邮件非常容易,以至于不需要一个类?还是因为我没找到?

如果我能够编写这样的类,当我对 python 足够好时,这对某人有用吗?

4

5 回答 5

6

如果你可以原谅一些公然的自我推销,我写了一个邮件模块,它使使用 Python 发送电子邮件变得相当简单。除了 Python smtplib 和电子邮件库之外,没有其他依赖项。

这是一个发送带有附件的电子邮件的简单示例:

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 添加了一个新版本的邮件程序,可让您在邮件程序类中指定端口。

于 2009-04-30T16:56:26.433 回答
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
于 2009-04-30T15:22:33.367 回答
2

只想指出我在找到此线程时正在寻找的Lamson 项目。我做了更多搜索并找到了它。它的:

Lamson 的目标是结束“电子邮件应用程序开发”的地狱。Lamson 并没有停留在 1970 年代,而是采用现代 Web 应用程序框架设计并使用经过验证的脚本语言 (Python)。

它与 Django 很好地集成。但它更适用于基于电子邮件的应用程序。虽然看起来像纯爱。

于 2010-02-16T21:18:21.090 回答
1

也许您可以尝试使用turbomail python-turbomail.org

它更容易和有用:)

import turbomail

# ...

message = turbomail.Message("from@example.com", "to@example.com", subject)
message.plain = "Hello world!"

turbomail.enqueue(message)
于 2009-05-21T21:45:25.700 回答
0

我建议阅读SMTP rfc。谷歌搜索表明,这可以通过使用您正在导入但从未使用的 MimeMultipart 类轻松完成。以下是Python 文档站点上的一些示例。

于 2009-04-30T15:00:06.963 回答