【问题标题】:PYTHON/OUTLOOK Sending e-mails through PYTHON with DOCXPYTHON/OUTLOOK 通过 PYTHON 和 DOCX 发送电子邮件
【发布时间】:2016-03-23 02:56:53
【问题描述】:

我必须通过 python 发送邮件。有用。快完成了。唯一的问题是我也必须保持格式。因此,要么我必须以 HTML 格式发送电子邮件(然后用 html 而不是 .docx 重写模板),要么复制带有扩展名的 .docx 文件 任何人有任何想法如何做到这一点?谢谢各位。

import win32com.client as win32
import fileinput as fi 
from docx import Document

outlook = win32.Dispatch('outlook.application')

path_in = 'maillist.csv'
input_file = open(path_in, 'r')
document = Document('template.docx')
document_html = open('template.html', 'r')

print(temp)
def filecount(fname):
        for line in fi.input(fname):
            pass
        return fi.lineno()

print("Total mails %s" % (filecount(path_in)))
count = 0
for line in input_file:
    if (count>16):
        name = line.split(";")[0]
        mail_adress = line.split(";")[1]
        subject = line.split(";")[2]
        print ("%s:%s:%s:" % (name, mail_adress, subject))
        mail = outlook.CreateItem(0)
        mail.To = mail_adress
        mail.Subject = subject
        mail.body = temp.replace("XXXNAMEXXX", name)
        mail.send
    else:
        count+=1

【问题讨论】:

    标签: python email outlook


    【解决方案1】:

    尝试将.RTFBody 和/或.HTMLBody 方法添加到文档对象:

        document = Document('template.docx').RTFBody
        document_html = open('template.html', 'r').HTMLBody
    

    另外,我不确定它是否有很大的不同,但为了惯例,我喜欢将 mailItem 对象的方法的第一个字母大写。

    让我知道这是否有效。

    【讨论】:

      猜你喜欢
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多