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

查看:222
本文介绍了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\roy\Royfile.csv'.但是我想要attachment = file,以便附件在每个for循环中针对不同的用户而更改.这部分不起作用.

Currently the script is using attachment = r'C:\Users\roy\Royfile.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 

第3个文件,例如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天全站免登陆