2

後置パイプから HTML メールを自動返信する Python スクリプトを作成したい: エイリアス ファイルの例:

testmail: "| python /opt/script/autoreply.py /opt/script/autoreply.html"

autoreply.html には HTML メールが含まれます。Python の例があります: https://docs.python.org/2/library/email-examples.html#id5

代替の MIMEMultipart を送信する

その例をスクリプトに使用したいと思います。アイデアは、autoreply.html ファイルをロードすることです。

#!/usr/bin/env python
#autoreply.py
import smtplib
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "you@email.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = open(sys.argv[1], 'r')
html = open(sys.argv[2], 'r')

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text.read(), 'plain')
text.close()
part2 = MIMEText(html.read(), 'html')
html.close()

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('smtprelay.gameloft.org')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

そのスクリプトを編集して、パイプが html (autoreply.html) メールを送信できるようにするにはどうすればよいですか? 送信元アドレスを元のメッセージのアドレスに変更し、autoreply.html を読み込みます。autoreply.txt ファイルがない場合は、デフォルトで autoreply.html を読み込みます。(python の例では text/plain が必要です)

誰かが私を助けることができますか?それは私にとって非常に複雑です。ありがとう!

4

0 に答える 0