简体   繁体   English

使用python通过outlook发送html文件作为电子邮件正文

[英]Send html file as body of email through outlook using python

I am trying to send a bokeh graph through outlook email as body. 我试图通过outlook电子邮件发送散景图作为正文。 And you know, bokeh graph , I have generated as an HTML file. 你知道,散景图,我已经生成了一个HTML文件。 The same I would like to send in the email embedded as the body of the email. 同样我想发送嵌入电子邮件正文的电子邮件。

I tried reading the HTML file reading using the read command and supplying the same as the htmlbody. 我尝试使用read命令读取HTML文件读取并提供与htmlbody相同的内容。 But, it comes as blank in the email. 但是,它在电子邮件中是空白的。 Nothing comes populated. 什么都没有填充。 Below is the code I tried. 以下是我试过的代码。

import win32com.client as win32
import psutil
import os
import subprocess
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'ABC@XYZ.com'
mail.Subject = 'Sent through Python'
html_url='C:/Users/ABC/Documents/XYZ/test.htm'
with open(html_url, 'r') as myfile:
     data=myfile.read()
mail.HTMLBody = data
mail.send

Then tried the below ... But still email body is blank.. Any idea what is going wrong ??? 然后尝试下面的...但仍然邮件正文是空白的..任何想法出了什么问题???

from bokeh.embed import components
from jinja2 import Template
from bokeh.resources import INLINE
from bokeh.plotting import figure
from bokeh.io import output_file,show,output_notebook

import win32com.client as win32
import psutil
import os
import subprocess

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'ABC@XYZ.com'
mail.Subject = 'Sent through Python'

def get_plot_components() :
   # build your plot here
    plot = figure()
    plot.circle([2,3,4],[5,6,7])
    show(plot)
    script, div = components(plot)
    return script, div

template = Template('''
       <div id='bokeh_plot_for_the_body'>
          {{ resources | safe }}
          {{ div | safe }}
          {{ script | safe }}
       </div>
                ''')

script, div = get_plot_components()
outlook_body = template.render(resources = INLINE.render(),
                               script = script,
                               div = div)
mail.HTMLBody = outlook_body
mail.send

I have no Windows machine to test but I think the problem may be that you are trying to embed a full HTML page generated by Bokeh into the body of another HTML file generated by Outlook. 我没有Windows机器可以测试,但我认为问题可能是你试图将Bokeh生成的完整HTML页面嵌入到Outlook生成的另一个HTML文件的主体中。 So you are getting: 所以你得到:

<!DOCTYPE html>
<html lang='en'>
    <head>
        <title>Outlook Message</title>
    </head>  
    <body>

        <!DOCTYPE html>
        <html lang='en'>
            <head>
                <title>Bokeh Plot</title>
            </head>  
            <body>
                <div id=plot>
                <script id=bokeh_script>
                </script>
                </div>
            </body>
        </html>

    </body>
</html>

What I would advice is to merge your bokeh script with the script sending the Outlook message by adding a function that returns the bokeh components like this: 我建议通过添加一个返回散景组件的函数将散景脚本与发送Outlook消息的脚本合并:

from bokeh.embed import components
from jinja2 import Template
from bokeh.resources import INLINE

import win32com.client as win32
import psutil
import os
import subprocess

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'ABC@XYZ.com'
mail.Subject = 'Sent through Python'
html_url='C:/Users/ABC/Documents/XYZ/test.htm'
with open(html_url, 'r') as myfile:
    data=myfile.read()

def get_plot_components()
   # build your plot here
   script, div = components(plot)
   return script, div

template = Template('''
       <div id='bokeh_plot_for_the_body'>
          {{ resources | safe }}
          {{ div | safe }}
          {{ script | safe }}
       </div>
''')

script, div = get_plot_components()
outlook_body = template.render(resources = INLINE.render(),
                               script = script,
                               div = div)
mail.HTMLBody = outlook_body
mail.send

You need to add encoding with open the template file. 您需要在打开模板文件时添加编码。

Example: 例:

html_url= open('C:/Users/ABC/Documents/XYZ/test.htm', encoding='utf16')
data=html_url.read()

That works for me. 这对我行得通。

Looks like this can't be achieved as no email clients allow to run scripts due to security threat. 由于安全威胁,没有电子邮件客户端允许运行脚本,因此无法实现这一点。 Only way out is attaching an html file or give a html link in the email. 唯一的出路是附加一个html文件或在电子邮件中提供一个HTML链接。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM