简体   繁体   English

通过 python 替换 email 中的值

[英]substitute values in email via python

substitute values in email via python通过 python 替换 email 中的值

I am NEW to python and trying to write a script that will send an email (In HTML) to a group of people.我是 python 的新手,我正在尝试编写一个脚本,将 email(在 HTML 中)发送给一群人。

This python script will execute a perl script that will read an excel file and save it in a.txt file, once the file is created the python script will open the txtFile and create an array of all the names: This python script will execute a perl script that will read an excel file and save it in a.txt file, once the file is created the python script will open the txtFile and create an array of all the names:

Here is the snip from the scripts这是脚本的片段

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

print array[2]
print array[1]

$./tempSendOnCall.py Branch number is 2011.06 07-15-11 Ash dy Joe Des $./tempSendOnCall.py 分支编号是 2011.06 07-15-11 Ash dy Joe Des

Once this part is over I am creating an email (In the same script) that will send it out and I need to replace the names of the oncall and lead on call persons with the results from the Array as this changes every week.一旦这部分结束,我将创建一个 email(在同一个脚本中)将其发送出去,我需要用阵列的结果替换待命人员的姓名和待命人员的姓名,因为每周都会发生变化。

Here is the email portion:这是 email 部分:

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()

I have everything working for me, but I can't find a way to replace the Array values, or date, within that email!!我有一切为我工作,但我找不到替换该电子邮件中的数组值或日期的方法!

Please help:-)请帮忙:-)

-guy -家伙

Allow me to direct your attention to the format string function请允许我将您的注意力引向格式字符串 function

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])

Of course, it will need access to your Array variable to work.当然,它需要访问您的 Array 变量才能工作。

You can use a dict to substitute your fields ( docs ).您可以使用dict替换您的字段( docs )。 Example:例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用零代替python中的缺失值 - Substitute zero for missing values in python 在python中,有没有办法自动替换缺失值? - In python, is there a way to automatically substitute for missing values? 如何在Python中将MAP中的值替换为字符串模板 - How to substitute values in a MAP to string template in Python 如何在python中的数学方程式中替换值? - How to substitute values in mathematics equations in python? 通过Python发送电子邮件 - Sending an Email via Python 如何在Python中将值从数组替换为列表中的值? - How to substitute values from array to values in list in Python? 如何使用 re.sub 根据 python 中的模式字典和替换值替换文本的某些部分? - How to substitute some part of a text based on a dictionary of patterns and substitute values in python using re.sub? python pandas在csv文件的特定列中删除行并替换值 - python pandas to drop lines and substitute values in specific columns of a csv file 如何解析文件并将环境变量替换为Python中的值 - How do I parse a file and substitute the environment variables with values in Python 使用 Python 在 SQL 语句中为 PostgreSQL 分配/替换值列表 - Assign/Substitute a list of values in a SQL statement for PostgreSQL using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM