Python 或 Powershell — 读取 csv 并排序和收集要在电子邮件中发送的数据

标签 python powershell csv sorting email

下面的 csv 示例描述了我正在做的事情

ID,Name,SupervisorName,SupervisorEmail,Action,Column5,Column6,Column7
123,Tim Jones,Sam Burk,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="384b5a4d4a53784b57555d4a59565c5755165b5755" rel="noreferrer noopener nofollow">[email protected]</a>,2,data,data,data
124,Mark Smith,Sam Burk,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8c9d8a8d94bf8c90929a8d9e919b9092d19c9092" rel="noreferrer noopener nofollow">[email protected]</a>,2,data,data,data
125,Jill Jones,Tim Jones,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c08161312190f3c0f1311190e1d12181311521f1311" rel="noreferrer noopener nofollow">[email protected]</a>,2,data,data,data
126,Sam Harris,Sam Burk,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="42312037302902312d2f2730232c262d2f6c212d2f" rel="noreferrer noopener nofollow">[email protected]</a>,3,data,data,data
127,Jana Lester,Lisa Jones,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7dbddd8d9d2c4f7c4d8dad2c5d6d9d3d8da99d4d8da" rel="noreferrer noopener nofollow">[email protected]</a>,2,data,data,data

我想要收集操作为 2 且主管电子邮件完全相同的数据,然后向主管发送一封电子邮件,其中包含与其关联的每个帐户的行数据,暗示操作 2 事件已发生已被触发。

发送的电子邮件基于操作。需要针对给定操作对给定主管的电子邮件的所有行进行分组,并向每个主管发送一封电子邮件,其中包含与操作 2 匹配的多行数据。

这是一个大约 30k 行的 CSV,每个主管可能有 5 个以上具有给定事件的帐户。

I want the output in the email to be something like:

Dear Sam Burk,

The below accounts have had the Action 2 performed on them. Please contact us with any concerns.

ID    Name         Action
123   Tim Jones     2
124   Mark Smith    2
126   Sam Harris    2

Regards,
Corporate Events

我假设我需要一个包含与 2 相关操作的所有主管电子邮件的唯一列表。有执行此操作的示例代码吗?

然后,我假设我将使用该列表并对每种类型语句执行一个操作,然后针对我需要的特定列使用操作 2 收集给定主管的所有行。然后用该数据撰写电子邮件,发送电子邮件,然后转到列表中的下一封...有关如何执行此操作的任何示例代码?

我可以用 pandas 来排序吗? 什么是一个好的导入/库来将电子邮件发送到需要身份验证的 SMTP 网关?

Powershell 也可能是一种选择。

谢谢, 弗雷德

最佳答案

使用 Import-Csv 的 PowerShell 解决方案和 Group-ObjectSend-MailMessage :

注意:

  • Send-MailMessage 用于简单起见,因为它随 PowerShell 一起提供,但它被认为已过时,因为它不能保证与 SMTP 服务器的安全连接.

  • official suggestion就是使用第三方MailKit图书馆代替; this article有背景信息。
    另一个第三方为此库创建了 PowerShell 包装器:Send-MailKitMessage .

# Fill in the appropriate values for your environment here.
$csvFile = 'file.csv' 
$smtpServer = 'smtp.example.org'
$senderEmail = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdaca9a0a4a38da8b5aca0bda1a8e3a2bfaa" rel="noreferrer noopener nofollow">[email protected]</a>'

Import-Csv $csvFile | Group-Object SupervisorEmail | ForEach-Object {

  # Compose the email body.
  $body = @"
Dear $($_.Group[0].SupervisorName)

The below accounts have had the Action 2 performed on them. Please contact us with any concerns.

$($_.Group | Select-Object ID, Name, Action | Out-String)

Regards,
Corporate Events
"@

  # Uncomment the following line to print the value of $body for diagnostic purposes:
  # Write-Verbose -Verbose $body

  # Define the parameters for the command that sends the email...
  $params = @{
    SmtpServer = smtpServer
    From = $senderEmail
    To = $_.Group[0].SupervisorEmail
    Subject = 'Action 2 Notification'
    Body = $body
  }

  # ... and send it.
  Send-MailMessage @params

}

关于Python 或 Powershell — 读取 csv 并排序和收集要在电子邮件中发送的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68967794/

相关文章:

python - Tensorflow:使用 Adam 优化器

c# - 从单独的线程更新 cmdlet 进度的最佳方法

c# - System.DirectoryServices.ActiveDirectory 和 Microsoft.ActiveDirectory 之间的区别

javascript - 如何解析 CSV 数据?

PHP 网页 - 在插入数据库之前预览 CSV 上传

python - 使用python删除曲线下方的数据点

python - 关于字符编码的 Python 和 Perl 打印差异

python - Python 中用于影子的 2y-blowfish 哈希键

powershell - 在Powershell中获取Windows中可用于Docker的全局最大内存

mysql - 使用 shell 脚本从 csv 文件批量复制到 OracleDB