Link to home
Start Free TrialLog in
Avatar of rakkad
rakkadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell script include embedded images

Hi

I have a powershell script that uses the send-mailmessage cmdlet, but the script needs to incorporate a nbs.bmp image as part of the message body, but can't seem to get this working.

Can anyone help please

Thank-you
Avatar of footech
footech
Flag of United States of America image

To my knowledge, this can only be done via use of .NET objects instead of using Send-MailMessage.
$SmtpServer = "smtp.yourdomain.com"
$To = "you@domain.com"
$From = "me@domain.com"
$Subject = "email with embedded image"
# The HTML content for the message body
$Body =  ""  #This is a placeholder.  Generate or retrieve your HTML from a file and assign it to the variable.  Must include the IMG tag, see the note below regarding ContentID
# The real path to the attachment
$AttachmentPath = "C:\temp\nbs.bmp"

$SmtpClient = New-Object System.Net.Mail.SmtpClient($SmtpServer)

# Argument order for constructor is From, To, Subject, Body
$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To, $Subject, $Body)
$MailMessage.IsBodyHtml = $true

# Sort out the attachment
$Attachment = New-Object System.Net.Mail.Attachment($AttachmentPath)
$Attachment.ContentID = "SomeImage"
# the name of the content ID has to match the IMG reference in the HTML body
# example reference:  <IMG SRC='cid:SomeImage' ALT='Image of something'>

# And add it onto the mail
$MailMessage.Attachments.Add($Attachment)

# Send the message
$SmtpClient.Send($MailMessage)

# Unlock the file (just a file-open lock)
$Attachment.Dispose()

Open in new window

Avatar of rakkad

ASKER

I have built my script around using send-mailmessage and i don't want to change the script by using net.objects.

Is there an alternative approach ?

Thanks
No, footech is right, if you want embedded attachments you need to use the .NET class which underpins Send-MailMessage.

There are pre-existing scripts kicking around which do exactly the same thing as footech's response, but you're just substituting the work you can get support on from footech with something someone else did (which, ultimately, will be the much the same anyway).
Avatar of rakkad

ASKER

Can I use a combination of both, so still remain the existing send-mailmessage command and use .net class as well ?

Thanks
Nope, sorry. This part of the functionality provided above is mandatory:
$Attachment = New-Object System.Net.Mail.Attachment($AttachmentPath)
$Attachment.ContentID = "SomeImage"

Open in new window

I do appreciate this is frustrating, unfortunately Send-MailMessage (like many, many other commands) doesn't flex to meet requirements like this.
What Chris is talking about is something like the modified Send-MailMessage at https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d .
Avatar of rakkad

ASKER

This relates to LVL69

The script that I have created looks for files in c:\cogtest which is ok, but then when it tries to send files it further looks for files in c:\windows\system32, so I am having to copy files to c:\windows\system32 and then it is ok.

Is this related to system.io.path that is defined in this script.  Can it be changed so it looks at c:\cogtest ?

Thanks
When you use paths with .NET types they do not honour the PowerShell path; they don't know anything about it. You end up in whatever is the working directory for the program you're using. In the case of Powershell that's quite frequently set to C:\Windows\System32.

There are a number of approaches you can take. For example:
(Get-Item $relativePath).FullName

Open in new window

Or:
(Resolve-Path $relativePath).Path

Open in new window

Exactly which approach is most appropriate depends on your code. It's a known aspect of working with .NET types which ingest paths in PowerShell.
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rakkad

ASKER

Thanks.  This helped me sort the issue out