Home > Net >  Change Powershell script to send the email with embedded text
Change Powershell script to send the email with embedded text

Time:07-22

I would like to modify the script below, to send the email with embedded text, without removing the html attachments.

The data it should embed into the email is found at: "C:\Users\Users\Summary\SummaryCSV.txt"

Here is the Powershell Script which needs to be modified:

 # Check to see we have all the arguments
 If (Test-Path -Path "C:\Users\Users\Summary\SummaryCSV.txt") {
 #Send Email with HTML as attachment and no text embedded
 $FullPath=$args[0] 
 #Get an Outlook application object
 $o = New-Object -com Outlook.Application
 $mail = $o.CreateItem(0)
 #2 = High importance message
 $mail.importance = 1
 $mail.subject = "Ready to be retrieved: $(get-date)"
 $mail.body = "Summary $(get-date)"
 #separate multiple recipients with a ";"
 $mail.To = "[email protected]"
 # Iterate over all files and only add the ones that have an .html extension
 $files = Get-ChildItem $FullPath
 for ($i=0; $i -lt $files.Count; $i  ) {
 $outfileName = $files[$i].FullName
 $outfileNameExtension = $files[$i].Extension
 # if the extension is the one we want, add to attachments
 if($outfileNameExtension -eq ".html")
 {
 $mail.Attachments.Add($outfileName);
 }
 } 
 $mail.Send()
 # give time to send the email
 Start-Sleep 5
 # quit Outlook
 $o.Quit()
 #end the script
 #exit
 }

Thanks in advance for your assistance.

CodePudding user response:

Based on your specifications:

# Check to see we have all the arguments
If ((Test-Path -Path C:\Users\Users\Summary\SummaryCSV.txt) -and (Test-Path -Path C:\Users\Users\Summary\SummaryCSV.txt)) {
#Send Email with HTML as attachment and no text embedded

$FullPath=$args[0] 
$embeddedText = Get-Content -Path C:\Users\Users\Summary\SummaryCSV.txt -Raw

#Get an Outlook application object
$o = New-Object -com Outlook.Application
$mail = $o.CreateItem(0)
#2 = High importance message
$mail.importance = 1
$mail.subject = "Ready to be retrieved: $(get-date)"
$mail.body = "Summary $(get-date)`r`n$embeddedText"

#separate multiple recipients with a ";"
$mail.To = "[email protected]"

# Iterate over all files and only add the ones that have an .html extension
$files = Get-ChildItem $FullPath

for ($i=0; $i -lt $files.Count; $i  ) {
    $outfileName = $files[$i].FullName
    $outfileNameExtension = $files[$i].Extension
    # if the extension is the one we want, add to attachments
    if($outfileNameExtension -eq ".html") {
        $mail.Attachments.Add($outfileName);
    }
}

$mail.Send()
# give time to send the email
Start-Sleep 5
# quit Outlook
$o.Quit()
 #end the script
 #exit

}

  • Related