title: PowerShell Send-MailMessage to multiple recipients with differing information in the body
body: <p>I have a sql script that pulls records into a csv based on specific criteria. The csv will typically look like this example:</p>
  
  <pre><code>Ticket,Last Updated,Priority,Assigned To,Email
  2329,1-12-2017,Medium,Assignee1,assignee1@domain.tld
  2417,5-23-2016,Low,Assignee1,assignee1@domain.tld
  2416,4-20-2017,Medium,Assignee2,assignee2@domain.tld
  2463,8-9-2016,Medium,Assignee2,assignee2@domain.tld
  2481,3-4-2017,Low,Assignee3,assignee3@domain.tld
  2499,2-22-2017,Medium,Assignee4,assignee4@domain.tld
  2500,12-20-2016,High,Assignee5,assignee5@domain.tld
  2498,11-30-2016,Medium,Assignee5,assignee5@domain.tld
  </code></pre>
  
  <p><strong><em>Note</strong> that the email field can have the same email address for multiple rows.</em></p>
  
  <p>I am having trouble understanding how I should parse this data in PowerShell so that I can send the row data to the address in the <code>Email</code> column for that row. </p>
  
  <p>I do not want to send multiple emails to the same person when this script runs however. So if the <code>Email</code> column has identical values in the csv I would like send one email with multiple rows of information in the body of the email before moving on to the next until EOF. </p>
  
  <p>In the example csv above only one email would go to <code>assignee1@domain.tld</code> for <code>Ticket 2329 &amp; 2417</code> data and so on until EOF. I am doing an <code>Order By Email</code> in the sql query that gets dumped to the csv if that helps organize the process.</p>
  
  <p>I have used PowerShell to send emails previously but those scripts do not need to parse the data for a specific email address as the <code>-to</code> in the <code>Send-MailMessage</code> has always been static. Example from another script:</p>
  
  <pre><code>$FromAddress    = "noreply@domain.tld"
  $ToAddress      = "something@domain.tld"
  $Subject        = "Mail message Subject line"
  $SMTPserver     = "smtp server address"
  
  
  if ((Get-Item "data.csv").length -gt 0kb)
  {
      $html = import-csv -Path .\data.csv | Foreach {$_.web_link= '&lt;a href="{0}"&gt;{1}&lt;/a&gt;' -f $_.web_link,$_.author;$_} | Foreach {$_.web_link= '&lt;a href="{0}"&gt;{1}&lt;/a&gt;' -f $_.web_link,$_.title;$_}| 
      Select @{Name="Link"; Expression={$_.web_link}}, @{Name="Author"; Expression={$_.web_link}} | ConvertTo-Html -Head $htmlformat -Body $bodyformat
      $html = $html -replace "&amp;lt;","&lt;" -replace "&amp;quot;",'"' -replace "&amp;gt;","&gt;"
      $body = $notice + $html
      Send-MailMessage -from $FromAddress -to $ToAddress -subject $Subject -bodyashtml ($body | Out-String) -priority High -smtpServer $SMTPserver 
  }
  else {exit;}
  </code></pre>
  
  <p>As a PowerShell novice (at best) I am wondering what my options are. I am assuming I will be looping through the csv quite a bit and potentially building out arrays. I just want to make sure, for my own education, that the task is accomplished in the simplest form PowerShell allots us.</p>
  
  <p>Thanks.</p>