I have a csv file, i want to send email separately to each user, currently from my script i am getting emails to all users addresses those are in my csv file "user" column. Here is my CSV file Data.
#TYPE System.Management.Automation.PSCustomObject
"time","remip","user","msg"
"11:12:15","124.29.228.164","[email protected]","SSL tunnel shutdown"
"11:12:43","124.29.228.164","[email protected]","SSL tunnel established"
"11:13:25","124.29.228.164","[email protected]","SSL tunnel established"
"11:14:05","202.63.194.8","[email protected]","SSL tunnel established"
This is my powershell code
$Subject = " Alerts $(get-date -Format "dddd MM-dd-yyyy HH:mm")"
$Server = "qs42.xyz.com"
$From = "[email protected]"
$To = $ID = Import-Csv myfile.txt | Select-Object -ExpandProperty user -Unique
$PWD = ConvertTo-SecureString "test123" -AsPlainText -force
$Cred = New-Object System.Management.Automation.PSCredential("[email protected]" , $pwd)
$path = "myfile.txt"
$file = (Import-Csv $path) | Select-Object -Unique -Property Time , remip , user , Msg
Send-MailMessage -From $from -to $To -Port 587 -SmtpServer $server -Subject $Subject -Credential $cred -UseSsl -Body ($file | Out-String)
Iam able to get emails of this data on both address [email protected], [email protected] which i dont want, i know emails are receiving because of my variable setting but my requirement is to get emails of each user data on the same user email address.
>>time remip user msg
---- ----- ---- ---
11:12:15 124.29.228.164 [email protected] SSL tunnel shutdown
11:12:59 124.29.228.164 [email protected] SSL tunnel shutdown
11:13:25 124.29.228.164 [email protected] SSL tunnel established
11:14:05 202.63.194.8 [email protected] SSL tunnel established
I don't know how to do this any help please.
CodePudding user response:
There were a couple of things wrong with your code:
$PWD
is an automatic variable and means the current working directory. You must not use that name as self-defined variable- You use
Import-Csv
multiple times where once is enough - The way you try to get a value in the
$To
variable is wrong - you need
Group-Object
because some users may have more than one message - I'd recommend using Splatting the parameters to the Send-MailMessage cmdlet
Try:
$password = ConvertTo-SecureString "test123" -AsPlainText -force
$cred = New-Object System.Management.Automation.PSCredential("[email protected]" , $password)
# import the data from the csv, group on field 'user' (which is the emailaddress to send to)
Import-Csv -Path 'D:\Test\myfile.txt' | Group-Object user | ForEach-Object {
# build a Hashtable to neatly send the parameters to Send-MailMessage cmdlet
# this is called Splatting.
$mailParams = @{
From = '[email protected]'
To = $_.Name # the Name of the group is from the user field in the csv
Port = 587
SmtpServer = 'qs42.xyz.com'
Subject = 'Alerts {0:dddd MM-dd-yyyy HH:mm}' -f (Get-Date)
Credential = $cred
UseSsl = $true
Body = $_.Group | Format-Table -AutoSize | Out-String
}
Send-MailMessage @mailParams
}
CodePudding user response:
# Import the necessary modules
Import-Module -Name Csv
Import-Module -Name SmtpClient
# Set the path to the CSV file
$csvFile = "path\to\file.csv"
# Read the CSV file
$data = Import-Csv -Path $csvFile
# Loop through each row in the CSV file
foreach ($row in $data) {
# Extract the email address and message from the row
$email = $row.user
$message = $row.msg
# Create an instance of the SmtpClient class
$smtp = New-Object -TypeName System.Net.Mail.SmtpClient
# Set the SMTP server and port
$smtp.Host = "smtp.example.com"
$smtp.Port = 25
# Create a new mail message
$mail = New-Object -TypeName System.Net.Mail.MailMessage
# Set the sender and recipient
$mail.From = "[email protected]"
$mail.To.Add($email)
# Set the subject and body
$mail.Subject = "SSL Tunnel Status"
$mail.Body = $message
# Send the email
$smtp.Send($mail)
# Print a message to the console
Write-Host "Sent email to $email"
}