PythonのphpMailer phpクラスはどこにありますか?
質問
私はpythonが初めてです。 実際、私はpythonで注目のメールを送信しようとしています:html本文、代替テキスト本文、添付ファイル。
したがって、 tutorial を見つけて改作しました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()
そしてそれは機能します..今では添付ファイルを逃すだけです.. そして、私は添付ファイルを追加することができません( this の投稿から)
では、なぜphpMailer for phpのような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 :コメントの1つにより、メーラークラスでポートを指定できるメーラーの新しいバージョンがpypiに追加されました。
他のヒント
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の目標は、「電子メールアプリケーション開発」という地獄に終止符を打つことです。 1970年代に留まるのではなく、ラムソンは最新のWebアプリケーションフレームワークデザインを採用し、実績のあるスクリプト言語(Python)を使用しています。
Djangoとうまく統合します。しかし、それは電子メールベースのアプリケーション向けに作られています。純粋な愛のように見えます。
たぶん turbomail python-turbomail.orgで試すことができます
より簡単で便利です:)
import turbomail
# ...
message = turbomail.Message("from@example.com", "to@example.com", subject)
message.plain = "Hello world!"
turbomail.enqueue(message)