Last active
September 5, 2018 17:55
-
-
Save constructor-igor/6c32a7221bb4496807e8 to your computer and use it in GitHub Desktop.
Powershell: send email via Outlook and insert image to body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SendEmailWithEmbeddedImage([String] $to, [String] $subject, [String] $body, [String] $attachment, [String] $imageFile) | |
{ | |
#$htmlHeader = "<HEAD>Text<B>BOLD</B> <span style='color:#E36C0A'>Color Text</span></HEAD>" | |
$htmlText = "<HTML>{0}</HTML><img src='cid:{1}'>" | |
$Outlook = New-Object -ComObject Outlook.Application | |
$Mail = $Outlook.CreateItem(0) | |
$Mail.To = $to #"[email protected]" | |
$Mail.Subject = $subject | |
$Mail.HTMLBody = [string]::Format($htmlText, $body, ExtractFileName $imageFile) | |
#$Mail.Body = $body | |
$Mail.Attachments.Add($attachment) | |
$Mail.Attachments.Add($imageFile) | |
$Mail.Send() | |
} | |
function ExtractFileName([String] $filePath) | |
{ | |
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath) | |
$fileExt = [System.IO.Path]::GetExtension($filePath) | |
$result = [string]::format("{0}{1}", $fileName, $fileExt) | |
return $result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment