【问题标题】:Send html file as body of email through outlook using python使用 python 通过 Outlook 将 html 文件作为电子邮件正文发送
【发布时间】:2022-03-29 14:00:54
【问题描述】:

我正在尝试通过 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

【问题讨论】:

    标签: python python-3.x email outlook bokeh


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我没有要测试的 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
      

      【讨论】:

        【解决方案3】:

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

        例子:

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

        这对我有用。

        【讨论】:

          【解决方案4】:

          如果任何寻找答案的人只在电子邮件正文中发送 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()
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-09-14
            • 2018-05-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-01-18
            • 1970-01-01
            相关资源
            最近更新 更多