2

我想创建一个 python 脚本来从后缀管道自动回复 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 的示例需要文本/纯文本)

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

4

0 回答 0