powershell - Inserting variables into html body using Send-MailMessage -
i have no issues sending html email using send-mailmessage, there way can insert value of variable?
csv:
email,samaccountname,newsam, jdoe@amce.com,john.doe,jdoe,
command:
$html = (get-content .\email.html) import-csv .\my-file.csv | foreach-object { send-mailmessage -smtpserver 10.0.0.1 -subject "subject here" -from admin@acme.com -to $_.email -priority: high -bodyashtml "$html" }
html body:
<html> <head> <title>html generator sample page</title> </head> <body> <p> <span style="font-family:times new roman,times,serif;"><span style="font-size: 12px;"><strong>old username: $_.samaccountname </strong></span></span></p> <p> <span style="font-family:times new roman,times,serif;"><span style="font-size: 12px;"><strong>new username: $_.newsam </strong></span></span></p> </body> </html>
the email body shows $_.newsam instead of value imported .csv file. in advance assistance!
you have substitute placeholders yourself.
change html body (note .net string formatting placeholders):
<html> <head> <title>html generator sample page</title> </head> <body> <p> <span style="font-family:times new roman,times,serif;"><span style="font-size: 12px;"><strong>old username: {0} </strong></span></span></p> <p> <span style="font-family:times new roman,times,serif;"><span style="font-size: 12px;"><strong>new username: {1} </strong></span></span></p> </body> </html>
then, change code this:
$html = get-content -path $psscriptroot\email.html -raw; # html text 1 line import-csv -path $psscriptroot\my-file.csv | foreach-object { send-mailmessage -smtpserver 10.0.0.1 -subject "subject here" -from admin@acme.com -to $_.email -priority: high -bodyashtml ($html -f $_.samaccountname, $_.newsam); }
note: in powershell v3.0 , later, $_
, $psitem
can used interchangeably.
Comments
Post a Comment