Home > database >  I want to inject the variables in the $emp_first_name the body of the email
I want to inject the variables in the $emp_first_name the body of the email

Time:12-07

I have a csv file with emp_first_name,Emp_Last_Name, employee#, upn, ManagerEmail. Column examples "jon,doe,99,[email protected],[email protected]" i would like to send an email to the email addresses in the managermail column saying new employee's accounts are ready.

I want to inject the variables in the $emp_first_name the body of the email. I have it working without the variables. Thank to anyone who can help.

 $infile = "c:\sdk\import.csv"
 $userlist = import-csv $infile

 $subject= "New Employee Account"

 $username = "[email protected]"
 $password = "$%t67Bgt##23$%t67Bgt##23hhgfhfghfghgfhgffgfgdhfghfggffgdgfdhgfhfgffsd"        
 $sstr = ConvertTo-SecureString -string $password -AsPlainText -Force
 $cred = New-Object System.Management.Automation.PSCredential -argumentlist $username, 
  $sstr
 $Emp_First_Name = $user.emp_first_name
 $Emp_Last_Name = $user.Emp_Last_Name
 $employee = $user.employee
 $upn = $user.upn
 $ManagerEmail = $user.ManagerEmail
 $Body="

 Hello

 The account for  $Emp_First_Name $Emp_Last_Name employee number $employee is ready. Her 
 user email and username is $upn.
  "

foreach ($user IN $userList) {

Send-MailMessage –From [email protected] –To $ManagerEmail –Subject $subject –Body $body  - 
SmtpServer smtp.office365.com -Credential $cred -UseSsl -Port 587
 }

CodePudding user response:

If string interpolation doesn't happen automatically, you can force it using the ExpandString method:

For example you might do something like this:

$employee = 12345
$Emp_First_Name = 'Jane'
$Emp_Last_Name = 'Doe'
$upn = '[email protected]'

$bodyTemplate = 
"Hello

The account for $Emp_First_Name $Emp_Last_Name employee number $employee is ready.
Her user email and username is $upn."

$body = $ExecutionContext.InvokeCommand.ExpandString($bodyTemplate)

Send-MailMessage -Body $body -From ....

In this case the contents of $body would be:

Hello

The account for Jane Doe employee number 12345 is ready.
Her user email and username is [email protected].

Note that unless you are only dealing with women, you might want to use a more gender-neutral pronoun in your template :-)

Note: This likely won't work for more complex situations, but is fine for simple text replacement like this.

CodePudding user response:

You can do string interpolation with the format operator -f, also your code would benefit greatly using splatting.

# $subject and $cred should be defined here as you already have them

$template = @'
Hello
The account for {0} {1} employee number {2} is ready.
Her user email and username is {3}.
'@

$mailParams = @{
    From       = '[email protected]'
    Subject    = $subject
    SmtpServer = 'smtp.office365.com'
    Credential = $cred
    UseSsl     = $true
    Port       = 587
}

foreach ($user in Import-csv "c:\sdk\import.csv") {
    $body = $template -f @(
        $user.emp_first_name
        $user.Emp_Last_Name
        $user.employee
        $user.upn
    )

    $mailParams['To'] = $user.ManagerEmail
    $mailParams['Body'] = $body

    Send-MailMessage @mailParams
}
  • Related