Link to home
Start Free TrialLog in
Avatar of Radim88
Radim88

asked on

send-mailmessage (powershell command-let) is possible to add picture to this email?

Hello,

I have this script for telling users to change their password before it expires. Do you think it would be possible to add somehow a comapny logo or some picture into it?

Many thanks

$date = (get-date).adddays(40)
get-qaduser username | %{

if ($_.passwordexpires -lt $date){

$email =  $_.email
$accountexpires = $_.passwordexpires.Tostring(“dd.MM.yyyy HH:mm:ss”)

send-mailmessage -to $email -from username@xxxx.com   -smtpserver smtpserver -body "Hello, your account expires on $accountexpires , please change your password! "  -subject "Account Expiration Notice"
}
}
Avatar of KenMcF
KenMcF
Flag of United States of America image

If you know HTML You can use the -bodyasHtml switch and create you own template.
Avatar of Chris Dent

Yes... but...

You have to write the body of the message as HTML (and feed it the -BodyAsHtml parameter). There are two ways to insert images:

1. Link to an external resource. e.g.

<img src='http://somepublicserver/filename.jpg' />

2. Embed it. You'd have to back away from Send-MailMessage and build it all yourself (see the untested example below).

The attachment object needs the content type set, and a unique ID the ContentDisposition property. Finally, the image is referenced by  the CID in the HTML source. e.g.


$Attachment = New-Object Net.Mail.Attachment("C:\temp\img.jpg")
$Attachment.ContentDisposition.Inline = $True
$Attachment.ContentDisposition.DispositionType = "Inline"
$Attachment.ContentType.MediaType = "image/jpg"

$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To = "you@domain.com"
$MailMessage.From = "you@domain.com"
$MailMessage.Subject = "test"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.Add($Attachment)

$MailMessage.Body = "
  <html>
    <head></head>
    <body>
      <img src='CID:$($Attachment.ContentId)' />
    </body>
  </html>"

$SmtpClient = New-Object Net.Mail.SmtpClient("TheMailServer", 25)
$SmtpClient.Send($MailMessage)


HTH

Chris

Sorry to repeat the bit about HTML requirements. It took me that long to finish up the example.

Chris
Avatar of Radim88
Radim88

ASKER

Hi, added it to my script,
And gives me error:
-------------
"To" is a ReadOnly property.
At :line:20 char:13
+ $MailMessage. <<<< To = $email

-----------------------
$date = (get-date).adddays(40)
get-qaduser username | %{

if ($_.passwordexpires -lt $date){

$email =  $_.email
$accountexpires = $_.passwordexpires.Tostring(“dd.MM.yyyy HH:mm:ss”)


$Attachment = New-Object Net.Mail.Attachment("C:\1.jpg")
$Attachment.ContentDisposition.Inline = $True
$Attachment.ContentDisposition.DispositionType = "Inline"
$Attachment.ContentType.MediaType = "image/jpg"


$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To = $email
$MailMessage.From = "username@xxxx.com"
$MailMessage.Subject = "Account Expiration Notice"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.Add($Attachment)

$MailMessage.Body = "
  <html>
    <head></head>
    <body>
      <img src='CID:$($Attachment.ContentId)' />
    </body>
  </html>"

$SmtpClient = New-Object Net.Mail.SmtpClient("smtpserver", 25)
$SmtpClient.Send($MailMessage)




}

}

Oops, sorry. Changed to:

$MailMessage.To.Add($email)

Chris
$date = (get-date).adddays(40)
get-qaduser username | %{

if ($_.passwordexpires -lt $date){

$email =  $_.email
$accountexpires = $_.passwordexpires.Tostring(“dd.MM.yyyy HH:mm:ss”)


$Attachment = New-Object Net.Mail.Attachment("C:\1.jpg")
$Attachment.ContentDisposition.Inline = $True
$Attachment.ContentDisposition.DispositionType = "Inline"
$Attachment.ContentType.MediaType = "image/jpg"


$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To.Add($email)
$MailMessage.From = "username@xxxx.com"
$MailMessage.Subject = "Account Expiration Notice"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.Add($Attachment)

$MailMessage.Body = "
  <html>
    <head></head>
    <body>
      <img src='CID:$($Attachment.ContentId)' />
    </body>
  </html>"

$SmtpClient = New-Object Net.Mail.SmtpClient("smtpserver", 25)
$SmtpClient.Send($MailMessage)

}
}

Open in new window


There is one more bit we need actually. $Attachment will leave you with a locked file. We need to Dispose of the attachment object at the end.

Chris
$date = (get-date).adddays(40)
get-qaduser username | %{

if ($_.passwordexpires -lt $date){

$email =  $_.email
$accountexpires = $_.passwordexpires.Tostring(“dd.MM.yyyy HH:mm:ss”)


$Attachment = New-Object Net.Mail.Attachment("C:\1.jpg")
$Attachment.ContentDisposition.Inline = $True
$Attachment.ContentDisposition.DispositionType = "Inline"
$Attachment.ContentType.MediaType = "image/jpg"


$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To.Add($email)
$MailMessage.From = "username@xxxx.com"
$MailMessage.Subject = "Account Expiration Notice"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.Add($Attachment)

$MailMessage.Body = "
  <html>
    <head></head>
    <body>
      <img src='CID:$($Attachment.ContentId)' />
    </body>
  </html>"

$SmtpClient = New-Object Net.Mail.SmtpClient("smtpserver", 25)
$SmtpClient.Send($MailMessage)

$Attachment.Dispose()

}
}

Open in new window

Avatar of Radim88

ASKER

thats really nice Chris. But would be possible to add it for example to the bottom of mail body?
mailmessage.png
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 Radim88

ASKER

Hello,

By this I actually dont receive any email.

Does the $SmtpClient.Send method throw an error message?

Otherwise you might check message tracking and / or junk mail to see if it's actually been attempted.

Chris
Avatar of Radim88

ASKER

Hello,
Now it looks like this:
mail.png
Avatar of Radim88

ASKER

Hello,

And do you think its possible to somehow avoid clicking preview on the attachment to show picture?
But on the other side its very good.


mail.png
Avatar of Radim88

ASKER

soory this is correct one
Untitled.png
Avatar of Radim88

ASKER

Hello,

I have this code:
But I am not sure how to correctly close HTML part into "" or ''
Error message is at the end:
Please check:

$date = (get-date).adddays(40)
get-qaduser username | %{

if ($_.passwordexpires -lt $date){

$email =  $_.email
$accountexpires = $_.passwordexpires.Tostring(“dd.MM.yyyy HH:mm:ss”)


$Attachment = New-Object Net.Mail.Attachment("C:\1.jpg")
$Attachment.ContentDisposition.Inline = $True
$Attachment.ContentDisposition.DispositionType = "Inline"
$Attachment.ContentType.MediaType = "image/jpg"


$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To.Add($email)
$MailMessage.From = "email"
$MailMessage.Subject = "Account Expiration Notice"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.Add($Attachment)

$MailMessage.Body = '
  <HTML xmlns:o = "urn:schemas-microsoft-com:office:office" xmlns:v = "urn:schemas-microsoft-com:vml">
    <head>
      <style type='text/css'>
        #BottomRight {
          position: absolute;
          bottom: 2px;
          right: 4px;
        }
      </style>
    </head>

    <body>


<H3 style="MARGIN: 10pt 0cm 0pt"><SPAN style="mso-no-proof: yes"><v:shapetype id=_x0000_t75 coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype><v:shape style="WIDTH: 2113px; HEIGHT: 258px; VISIBILITY: visible; mso-wrap-style: square" id=obrázek_x0020_2 o:spid="_x0000_i1028" type="#_x0000_t75">


<v:imagedata src="--address with picture---"></v:imagedata></v:shape></SPAN></H3>



      <p>Hello, your account expires on $accountexpires, please change your password!</p>
      <div id='BottomRight'>
        <img src='CID:$($Attachment.ContentId)' />
      </div>
    </body>
  </html>'
 



$SmtpClient = New-Object Net.Mail.SmtpClient("mailserver", 25)
$SmtpClient.Send($MailMessage)

$Attachment.Dispose()

}
}

------------------

This is error message:

Unexpected token 'urn:schemas-microsoft-com:office:office" xmlns:v = "urn:schemas-microsoft-com:vml">
    <head>
      <style type=`'text/css`'>
        #BottomRight {
          position: absolute;
          bottom: 2px;
          right: 4px;
        }
      </style>
    </head>

    <body>


<H3 style="MARGIN:' in expression or statement.
At :line:38 char:19
+   <HTML xmlns:o = " <<<< urn:schemas-microsoft-com:office:office" xmlns:v = "urn:schemas-microsoft-com:vml">






Avatar of Radim88

ASKER

thx