Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save constructor-igor/6c32a7221bb4496807e8 to your computer and use it in GitHub Desktop.
Save constructor-igor/6c32a7221bb4496807e8 to your computer and use it in GitHub Desktop.
Powershell: send email via Outlook and insert image to body
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 #"boss@company.com"
$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