【问题标题】:Send Outlook email with attachment to list of users in Excel with python使用 python 将带有附件的 Outlook 电子邮件发送到 Excel 中的用户列表
【发布时间】:2020-12-05 16:40:27
【问题描述】:

我可以使用带有以下脚本的 Outlook 发送电子邮件,但如果我尝试发送附件,则会出错。

文件名_Email.xlsx

NAME    EMAIL
Roy     Roy@gmail.com
Jack    Jack@gmail.com

Python 脚本

import win32com.client as win32
import pandas as pd

email_list = pd.read_excel(r'C:\Users\roy\Name_Email.xlsx')

names = email_list['NAME']
emails = email_list['EMAIL']

for i in range(len(emails)):
   name = names[i]
   email = emails[i]

   outlook = win32.Dispatch('outlook.application')
   mail = outlook.CreateItem(0)
   mail.To = email
   mail.Subject = 'Message subject'
   mail.Body = 'Hello ' + name
   attachment = "hosts.txt"
   mail.Attachments.Add(attachment)
   mail.Send()

我得到的错误:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Cannot find this file. Verify the path and file name are correct.', None, 0, -2147024894), None)

文件名正确,与脚本在同一目录下。

我也尝试更改附件 = 'hosts.txt',但同样的错误。不确定缺少什么。

【问题讨论】:

    标签: python email outlook win32com


    【解决方案1】:

    试试这个

    from win32com.client import Dispatch
    import win32com
    import pandas as pd
    
    def mailprepare():
        num = range(0, 5)
        for kk in num:
            outlook = win32com.client.Dispatch("Outlook.Application")
            for accoun in outlook.Session.Accounts:
                if accoun.SmtpAddress == 'your@mail.com':
                    newaccount = accoun
                    break
            mail = outlook.CreateItem(0)
            mail._oleobj_.Invoke(*(64209, 0, 8, 0, newaccount))
            data = pd.ExcelFile('D:\\path.xlsx')
            sheet = data.parse('Sheet1')
            name = sheet['Names'][kk]
            mailto = sheet['Mails'][kk]
    
            att = 'D:\\yourattchment.txt'
            with open(att, 'r') as my_attch:
                myfile=my_attch.read()
    
            mail.To = mailto
            mail.Subject = 'Subject'
            mail.Body = 'mail body'+ name
            mail.Attachments.Add(att)
    
            mail.Display(True)
            mail.send
    
    mailprepare()
    

    【讨论】:

    • 我对此有另一个问题,因为我希望附件从 for 循环中获取值。该脚本与静态路径/文件完美配合,但不适用于 for 循环中的变量文件。请告知,因为我在这里发布了问题stackoverflow.com/questions/63472575/…
    【解决方案2】:

    您只是将 hosts.txt 分配给一个变量,但 Attachments.Add 正在寻找带有文件名的完整文件系统路径

    要获取当前目录,请使用

    例子

    import os
    print(os.getcwd() + "\hosts.txt")
    

    或者

    import os
    
    attachment = "hosts.txt"
    print(os.path.realpath(attachment))
    

    仅供参考 - 我使用的是 3.8 python

    【讨论】:

      【解决方案3】:

      不建议使用 win32com 发送电子邮件(或其他任何东西)。它将程序与您的系统联系起来。例如,您将无法从 linux 系统或未安装 Outlook 的 Windows 系统运行此脚本。

      SMTP 是发送电子邮件的标准。尝试发送本文中所示的电子邮件https://school.geekwall.in/p/BJb4hizyz/how-to-send-emails-using-python 您需要稍作修改才能从 excel 文件中读取。我以前用过它,它很健壮且跨平台(与 win32com 不同)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-16
        • 1970-01-01
        • 2019-07-07
        • 2021-11-24
        • 2019-09-25
        • 2014-11-09
        • 2017-02-04
        相关资源
        最近更新 更多