通过python替换电子邮件中的值

通过python替换电子邮件中的值,python,Python,通过python替换电子邮件中的值 我是python新手,正在尝试编写一个脚本,向一群人发送电子邮件(HTML) 此python脚本将执行一个perl脚本,该脚本将读取excel文件并将其保存在.txt文件中,一旦创建该文件,python脚本将打开txt文件并创建一个包含所有名称的数组: 这是脚本中的剪报 file = open(txtFile, "r" ) array = [] for line in file: array.append( line ) print array[2]

通过python替换电子邮件中的值

我是python新手,正在尝试编写一个脚本,向一群人发送电子邮件(HTML)

此python脚本将执行一个perl脚本,该脚本将读取excel文件并将其保存在.txt文件中,一旦创建该文件,python脚本将打开txt文件并创建一个包含所有名称的数组:

这是脚本中的剪报

file = open(txtFile, "r" )
array = []
for line in file:
    array.append( line )

print array[2]
print array[1]
$./tempSendOnCall.py 分行编号为2011.06 07-15-11 灰烬 乔·德斯

这一部分结束后,我将创建一封电子邮件(使用相同的脚本)发送出去,我需要用数组中的结果替换oncall和lead-on-call人员的姓名,因为这每周都会发生变化

以下是电子邮件部分:

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()
sendmail=“/p4/sendmail”
SERVER=“ServerName”
从email.mime.multipart导入MIMEMultipart
从email.mime.text导入MIMEText
我=”name@domain"
你=”name@domain"
msg=MIMEMultipart('alternative')
msg['Subject']=“3LS/QA/SA随叫随到审查”
msg['From']=我
msg['To']=您
text=“测试”
html=”“”\
消息
Hi-Ash dy,(需要从阵列[1]更新)
从日期开始(应从日期开始准备=time.strftime(“%m-%d-%y”))
Joe Des(应替换为数组[2])是本周的3LS销售线索。\n
如果你有任何问题,请告诉我,
"""
part1=MIMEText(文本“纯”)
part2=MIMEText(html,'html')
附加信息(第1部分)
附加信息(第2部分)
s=smtplib.SMTP(服务器)
s、 sendmail(我,你,msg.as_string())
s、 退出
我的一切都很好,但我找不到一种方法来替换该电子邮件中的数组值或日期

请帮忙:-)


-guy

请允许我将您的注意力引向字符串函数

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])
html=”“”\
消息
Hi{0:},(需要从数组[1]更新)
从{1:}开始(应该从date=time.strftime(“%m-%d-%y”)准备就绪)
{2:}(应替换为数组[2])是本周的3LS销售线索。\n
如果你有任何问题,请告诉我,
“”格式(数组[1],时间.strftime(“%m-%d-%y”),数组[2])

当然,它需要访问数组变量才能工作。

您可以使用
dict
替换字段()。例如:

>>> 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'

非常感谢大家!!!我用过斯宾塞的方法,效果很好!!!因为我有4个字符串需要替换,所以我使用了这个.format(数组[1],数组[2],time.strftime(“%m-%d-%y”),分支)谢谢