win32com.client 发送不同的附件而不是固定的文件路径 [英] win32com.client to send different attachments instead of fixed file path

查看:37
本文介绍了win32com.client 发送不同的附件而不是固定的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下单个附件脚本向 Outlook 上的不同收件人发送电子邮件,但是如果我尝试使用 for 循环向每个用户发送不同的附件,则它会失败.

I am able to send email to different recipients on outlook with below script for single attachment, but if I try to send different attachments to each user using for loop, then it fails.

当前脚本使用 attachment = r'C:Users oyRoyfile.csv'.但我想要 attachment = file,以便不同用户在每个 for 循环中更改附件.这部分不起作用.

Currently the script is using attachment = r'C:Users oyRoyfile.csv'. But I want attachment = file, so that the attachment changes in each for loop for different users. This part is not working.

不同用户的不同文件,例如下面的Royfile.csv.但是还有 50 多个这样的文件.

Different files for different users, example Royfile.csv below. But there are 50 more such files.

Folder        FolderOwner    EmailAddress      AttachmentPath
C:folder1   Roy            Roy@gmail.com     Royfile.csv
D:folder2   Roy            Roy@gmail.com     Royfile.csv

同一文件夹中的第二个文件Jackfile.csv:

2nd file in same folder Jackfile.csv:

Folder        FolderOwner    EmailAddress      AttachmentPath  
C:folder3   Jack            Jack@gmail.com   Jackfile.csv
D:folder4   Jack            Jack@gmail.com   Jackfile.csv 

第三个文件,例如 Mandyfile.csv.以同样的方式在同一文件夹中为 50 个用户总共 50 个文件.

3rd file for example Mandyfile.csv. And same way total 50 files for 50 users in same folder.

Folder        FolderOwner    EmailAddress        AttachmentPath
C:folder5   Mandy            Mandy@gmail.com   Mandyfile.csv
D:folder6   Mandy            Mandy@gmail.com   Mandyfile.csv

Python 脚本

import glob, as
import win32com.client as win32
import pandas as pd

for file in glob.glob("*file.csv"):
    print(file)

    email_list = pd.read_csv(file)
    names = email_list['FolderOwner']
    emails = email_list['EmailAddress']
    attachments = email_list['AttachmentPath']

    for i in range(len(emails)):
       print(file)
       name = names[i]
       email = emails[i]
       attachment = r'{}.csv'.format(attachments)
       with open(attachment, 'r') as my_attachment:
          myfile = my_attachment.read()
    
       outlook = win32.Dispatch('outlook.application')
       mail = outlook.CreateItem(0)
       mail.To = email
       mail.Subject = 'Message subject'
       mail.Body = 'Hello ' + name
       mail.Attachments.Add(attachment)
       mail.Send()
       break

如果我删除附件部分,脚本的当前输出:

Current output of the script if I remove the attachment part:

Royfile.csv
Royfile.csv
Jackfile.csv
Jackfile.csv
Mandyfile.csv
Mandyfile.csv
...
..
.

现在正在为附件而苦苦挣扎 = ???.这样每个文件都会发送给 50 个用户.

Struggling now with what needs to be for attachment = ???. So that each file gets sent to 50 users.

推荐答案

我的问题终于找到答案了,下面是完整代码.错误即将到来,因为缺少 PATH.即使脚本与附件在同一文件夹中运行,win32com lib 也需要完整路径.现在工作完美.:)

Found answer for my question finally, below is full code. The error was coming, as there was PATH missing. win32com lib need full path even if the script is running in same folder as the attachments. works perfectly now. :)

import glob, as
import win32com.client as win32
import pandas as pd

for file in glob.glob("*file.csv"):
    print(file)

    email_list = pd.read_csv(file)
    names = email_list['FolderOwner']
    emails = email_list['EmailAddress']
    attachments = email_list['AttachmentPath']
    PATH = "C:\Users\roy\myfolder\"

    for i in range(len(emails)):
       print("Sending email with " + file)
       name = names[i]
       email = emails[i]
       attachment = attachments[i]
       attachment1 = PATH + attachment 
       with open(attachment1, 'r') as my_attachment:
          myfile = my_attachment.read()
    
       outlook = win32.Dispatch('outlook.application')
       mail = outlook.CreateItem(0)
       mail.To = email
       mail.Subject = 'Message subject'
       mail.Body = 'Hello ' + name
       mail.Attachments.Add(attachment1)
       mail.Send()
       break

这篇关于win32com.client 发送不同的附件而不是固定的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆