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

并且有效..现在只会错过附件.. 我无法添加附件(来自帖子)

那么,为什么php没有像phpMailer这样的python类呢? 是因为,对于一个中等能力的python程序员发送带有附件和alt文本体的html电子邮件是如此简单,以至于不需要课程? 或者是因为我没找到它?

如果我能够写一个这样的课程,那么当我对python足够好的时候,这对某人有用吗?

有帮助吗?

解决方案

如果你可以原谅一些公然的自我推销,我写了一个邮件模块,使用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中添加了一个新版本的邮件程序,可让您在Mailer类中指定端口。

其他提示

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

只想指向 Lamson Project ,这是我在找到此主题时所寻找的内容。我做了一些搜索并找到了它。它是:

  

Lamson的目标是结束“电子邮件应用程序开发”的地狱。 Lamson采用现代Web应用程序框架设计并使用经过验证的脚本语言(Python),而不是在20世纪70年代陷入困境。

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

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

这更容易和有用:)

import turbomail

# ...

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

turbomail.enqueue(message)

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top