首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python类在哪里等同于phpMailer?

Python类在哪里等同于phpMailer?
EN

Stack Overflow用户
提问于 2009-04-30 14:48:42
回答 5查看 2.9K关注 0票数 3

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

所以,我找到了这个tutorial,并对它进行了gmail身份验证(教程找到了here)。

我的自动取款机密码是:

代码语言:javascript
复制
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()

这很有效..。现在只剩下附件了..我无法添加附件(来自this帖子)

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

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

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-04-30 16:56:26

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

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

代码语言:javascript
复制
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的示例。:)

代码语言:javascript
复制
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中添加了一个新版本的邮件程序,可以让你在邮件类中指定端口。

票数 6
EN

Stack Overflow用户

发布于 2009-04-30 15:22:33

Django包含您在核心中需要的类docs here

代码语言:javascript
复制
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()

在我的设置中我有:

代码语言:javascript
复制
#GMAIL STUFF
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'name@gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
票数 3
EN

Stack Overflow用户

发布于 2010-02-17 05:18:21

我只想指向Lamson Project,当我找到这个帖子的时候,它就是我要找的。我又找了找,找到了。它是:

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

它与Django很好地集成在一起。但它更多的是为基于电子邮件的应用程序设计的。不过,这看起来像是纯粹的爱。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/807302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档