通过python
替换电子邮件中的值我是python的新手,并尝试编写一个脚本,将一组电子邮件发送给一群人。
这个python脚本将执行一个perl脚本,它将读取一个excel文件并将其保存在.txt文件中,一旦创建了文件,python脚本将打开txtFile并创建一个包含所有名称的数组:
以下是脚本中的剪辑
file = open(txtFile, "r" )
array = []
for line in file:
array.append( line )
print array[2]
print array[1]
$ ./tempSendOnCall.py 分行号是2011.06 11年7月15日 Ash dy Joe Des
一旦这部分结束,我正在创建一个电子邮件(在相同的脚本中),它将发送出去,我需要用数组中的结果替换oncall和带电话人员的姓名,因为这会每周更改
以下是电子邮件部分:
sendmail="/p4/sendEmail"
SERVER = "ServerName"
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = "name@domain"
you = "name@domain"
msg = MIMEMultipart('alternative')
msg['Subject'] = "3LS / QA / SA on-call review"
msg['From'] = me
msg['To'] = you
text = "test"
html = """\
<html>
Message
Hi Ash dy, (Need to be updated from Array[1])
starting on DATE (Should ready from date = time.strftime("%m-%d-%y"))
Joe Des (Should be replaced with Array[2]) is the 3LS lead for this week.\n
Let me know if you have any questions,
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP(SERVER)
s.sendmail(me, you, msg.as_string())
s.quit()
我有一切为我工作,但我无法找到一种方法来替换该电子邮件中的数组值或日期!!
请帮助: - )
-guy
答案 0 :(得分:0)
请允许我将您的注意力转移到format字符串函数
html = """\
<html>
Message
Hi {0:}, (Need to be updated from Array[1])
starting on {1:} (Should ready from date = time.strftime("%m-%d-%y"))
{2:} (Should be replaced with Array[2]) is the 3LS lead for this week.\n
Let me know if you have any questions,
""".format(Array[1], time.strftime("%m-%d-%y"), Array[2])
当然,它需要访问您的Array变量才能工作。
答案 1 :(得分:0)
您可以使用dict
替换字段(docs)。例如:
>>> Array = ["foo", "bar", "baz"]
>>> "some text %(a)s --more text %(b)s at %(c)s" % {"a":Array[1], "b":time.strftime("%m-%d-%y"), "c":Array[2]}
'some text bar --more text 07-15-11 at baz'