【问题标题】:How to add 2 variables in the email body in win32com with Python?如何使用 Python 在 win32com 的电子邮件正文中添加 2 个变量?
【发布时间】:2014-09-19 02:13:39
【问题描述】:

我有这个电子邮件模板:

def email_tamplate(*args):
Format = { 'UNSPECIFIED' : 0, 'PLAIN' : 1, 'HTML' : 2, 'RTF'  : 3}
profile = "Outlook"
#session = win32com.client.Dispatch("Mapi.Session")
outlook = win32com.client.Dispatch("Outlook.Application")
#session.Logon(profile)
mainMsg = outlook.CreateItem(0)
mainMsg.To = "myemail@amazon.com"  
mainMsg.Subject = "Automated Crap Daily Update"
mainMsg.BodyFormat = Format['RTF']
mainMsg.HTMLBody = body2


mainMsg.Send()  #this line actually sends the email

并且想发送一封正文中包含 2 个表格的电子邮件。所以我有两个身体: 这是一个:

 eod = []
body2 = ['<html><body><table border="1" style="width:300px"><tr><td>Title Level</td></tr><tr><td>Source</td><td>Count</td></tr>']
header = [['Title Level']]

for row in cur:
    eod.append(row)


count=0
count2=0
for item in eod:
    body2[0]=body2[0]+"<tr><td>"+str(eod[count2][count])+"</td><td>"+str(eod[count2][count+1])+"</td></tr>"
    count2=count2+1

body2[0]=body2[0]+"</table></body></html>"
body2=body2[0]
globals().update(locals())

这是另一个:

eod = []
body = ['<html><body><table border="1" style="width:300px"><tr><td>Previous Day</td></tr><tr><td>Decision_Status</td><td>Count</td></tr>']
header = [['Prev Day']]

for row in cur:
    eod.append(row)



count=0
count2=0
for item in eod:
    body[0]=body[0]+"<tr><td>"+str(eod[count2][count])+"</td><td>"+str(eod[count2][count+1])+"</td></tr>"
    count2=count2+1

body[0]=body[0]+"</table></body></html>"
body=body[0]
globals().update(locals())

两者都是使用来自不同查询的数据创建的。 所以我希望能够发送电子邮件变量 body 和 boody2 的正文 关于如何实现这一点的任何想法?

谢谢

【问题讨论】:

    标签: python email win32com


    【解决方案1】:

    不需要 2 个主体,使用 .format() 并将无限数量的变量放入 1 个主体

    例子:

    text ='some text'
    table= pd.DataFrame([1,2,3])
    
    msg.HTMLBody = '''<br>Hello, see this text: 
        <br>{text}<br>and this table:<br>{table}'''.format(text=text, table=table)
    

    【讨论】:

      【解决方案2】:

      我刚刚解决了这个问题。碰巧我只需要连接body + body2。

      就这么简单。 不过谢谢!

      【讨论】:

      • +1 请接受您自己的答案或删除问题。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 2017-06-17
      • 1970-01-01
      相关资源
      最近更新 更多