python - 自动回复电子邮件

时间:2012-02-09 03:22:08

标签: python html email reply

我想创建一个python脚本来自动回复来自postfix管道的html邮件: 别名文件中的示例:

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)

有人能帮助我吗?这对我来说非常复杂。 谢谢!

0 个答案:

没有答案
相关问题