python-3.x 通过outlook发送html文件作为电子邮件的正文

umuewwlo  于 2023-04-22  发布在  Python
关注(0)|答案(4)|浏览(274)

我试图通过outlook电子邮件发送散景图作为主体。你知道,散景图,我已经生成为HTML文件。同样,我想在电子邮件中嵌入作为电子邮件的主体发送。
我尝试使用read命令阅读HTML文件,并提供与htmlbody相同的内容。但是,它在电子邮件中为空白。没有填充任何内容。下面是我尝试的代码。

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

然后尝试了下面的...但仍然电子邮件正文是空白的..有什么想法是出了什么问题???

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
oo7oh9g9

oo7oh9g91#

看起来这是无法实现的,因为没有电子邮件客户端允许运行脚本,由于安全威胁。唯一的出路是附加一个html文件或在电子邮件中给予一个html链接。

gblwokeq

gblwokeq2#

我没有Windows机器进行测试,但我认为问题可能是你试图将Bokeh生成的完整HTML页面嵌入到Outlook生成的另一个HTML文件的正文中。所以你会得到:

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

我的建议是,通过添加一个返回散景组件的函数,将散景脚本与发送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
neekobn8

neekobn83#

您需要在打开模板文件时添加编码。
示例:

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

这对我很有效

nfs0ujit

nfs0ujit4#

如果任何寻找答案的人只在电子邮件正文中发送HTML表格,那么下面给出的技巧就有效了:

  1. pip install pywin32
  2. import win32com.client
outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = mail_config.get('To')
mail.Subject = mail_config.get('Subject')
mail.HTMLBody = mail_config.get('HTML_body')
mail.Send()

相关问题