Kapgep
April 13, 2021, 7:08am
1
Hello everybody!
There is a file - C:\Temp\Exchange.csv next cut:
#TYPE Selected.System.Management.Automation.PSCustomObject
"DisplayName";"EmailAddresses";"Text"
"TestUsers";"Test@domain.ru";"Hello! TextTextTextTextTextTextTextText";
"TestUsers2";"Test3@domain.ru";"Hello!";
"TestUsers3";"Test3@domain.ru";"Hello, User!";
"TestUsers4";"Test4@domain.ru";"Hello! TextTextTextTextTextTextTextText";
Is it possible to implement sending to recipients of the column: “EmailAddresses” with column text content: “Text”?
Olaf
April 13, 2021, 7:37am
2
Of course it is.
First: Have you tried to search for some examples for tasks like this? I’m sure there are several of them.
Second: What have you tried so far? Please share your code if you have some and explain what’s not working as you may expect it and share the error if there are some.
BTW:
It’s probably just a copy and paste error but your example CSV content is not valid. You have trailing semikolons at the end of the lines with values.
Kapgep
April 14, 2021, 3:09am
3
$csv = Import-Csv -Path "C:\CSV.csv"
$Credential = Get-Credential
Foreach($Message in $csv){
$Recipient = $Message.EmailAddress
$GivenName = $Message.GivenName
$Surname = $Message.Surname
$Course = $Message.Course
$Grade = $Message.Grade
$Subject = "Thema - $Surname $GivenName"
Write-Host "Sending email to $GivenName $LastName"
Write-Host "Email Address: $Recipient"
$mailBody =
@"
<b>Hello,</b> $GivenName.</br>
Test.
</br>
"@
Send-MailMessage -Body $mailBody -BodyAsHtml -From "email@domain.ru" -Cc "email@domain.ru" -To $Recipient -Subject $subject -Encoding $([System.Text.Encoding]::UTF8) -Credential $Credential -SmtpServer "smtp.domain.ru"
}
Olaf
April 14, 2021, 7:17am
4
OK. And what is the question? Do you get some errors? Does it not work as expected?
I’d recommend to read up about splatting in PowerSehll. That could make your code a little easier to read.
$csv = Import-Csv -Path 'C:\CSV.csv'
$Credential = Get-Credential
Foreach ($Message in $csv) {
$mailBody =
@"
<b>Hello,</b> $($Message.GivenName).</br>
Test.
</br>
"@
$SendMailMessageParams = @{
Body = $mailBody
BodyAsHtml = $true
From = 'email@domain.ru'
To = $Message.EmailAddress
Cc = 'email@domain.ru'
Subject = "Thema - $($Message.Surname) $($Message.GivenName))"
Encoding = 'UTF8'
Credential = $Credential
SmtpServer = 'smtp.domain.ru'
}
Send-MailMessage @SendMailMessageParams
}
… of course that’s untested. so try before with test data!
1 Like