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.Tostrin g(“dd.MM.y yyy 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"
}
}
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.Tostrin
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"
}
}
If you know HTML You can use the -bodyasHtml switch and create you own template.
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:\te
$Attachment.ContentDisposi
$Attachment.ContentDisposi
$Attachment.ContentType.Me
$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To = "you@domain.com"
$MailMessage.From = "you@domain.com"
$MailMessage.Subject = "test"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.A
$MailMessage.Body = "
<html>
<head></head>
<body>
<img src='CID:$($Attachment.Con
</body>
</html>"
$SmtpClient = New-Object Net.Mail.SmtpClient("TheMa
$SmtpClient.Send($MailMess
HTH
Chris
Sorry to repeat the bit about HTML requirements. It took me that long to finish up the example.
Chris
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.Tostrin g(“dd.MM.y yyy HH:mm:ss”)
$Attachment = New-Object Net.Mail.Attachment("C:\1. jpg")
$Attachment.ContentDisposi tion.Inlin e = $True
$Attachment.ContentDisposi tion.Dispo sitionType = "Inline"
$Attachment.ContentType.Me diaType = "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.A dd($Attach ment)
$MailMessage.Body = "
<html>
<head></head>
<body>
<img src='CID:$($Attachment.Con tentId)' />
</body>
</html>"
$SmtpClient = New-Object Net.Mail.SmtpClient("smtps erver", 25)
$SmtpClient.Send($MailMess age)
}
}
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.Tostrin
$Attachment = New-Object Net.Mail.Attachment("C:\1.
$Attachment.ContentDisposi
$Attachment.ContentDisposi
$Attachment.ContentType.Me
$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To = $email
$MailMessage.From = "username@xxxx.com"
$MailMessage.Subject = "Account Expiration Notice"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.A
$MailMessage.Body = "
<html>
<head></head>
<body>
<img src='CID:$($Attachment.Con
</body>
</html>"
$SmtpClient = New-Object Net.Mail.SmtpClient("smtps
$SmtpClient.Send($MailMess
}
}
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)
}
}
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()
}
}
ASKER
thats really nice Chris. But would be possible to add it for example to the bottom of mail body?
mailmessage.png
mailmessage.png
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello,
By this I actually dont receive any email.
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
ASKER
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
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
ASKER
soory this is correct one
Untitled.png
Untitled.png
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.Tostrin g(“dd.MM.y yyy HH:mm:ss”)
$Attachment = New-Object Net.Mail.Attachment("C:\1. jpg")
$Attachment.ContentDisposi tion.Inlin e = $True
$Attachment.ContentDisposi tion.Dispo sitionType = "Inline"
$Attachment.ContentType.Me diaType = "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.A dd($Attach ment)
$MailMessage.Body = '
<HTML xmlns:o = "urn:schemas-microsoft-com :office:of fice" 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@5 xe" filled="f" stroked="f"><v:stroke joinstyle="miter"></v:stro ke><v:form ulas><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:p ath o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></v:p ath><o:loc k v:ext="edit" aspectratio="t"></o:lock>< /v:shapety pe><v:shap e 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></H 3>
<p>Hello, your account expires on $accountexpires, please change your password!</p>
<div id='BottomRight'>
<img src='CID:$($Attachment.Con tentId)' />
</div>
</body>
</html>'
$SmtpClient = New-Object Net.Mail.SmtpClient("mails erver", 25)
$SmtpClient.Send($MailMess age)
$Attachment.Dispose()
}
}
------------------
This is error message:
Unexpected token 'urn:schemas-microsoft-com :office:of fice" 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:off ice" xmlns:v = "urn:schemas-microsoft-com :vml">
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.Tostrin
$Attachment = New-Object Net.Mail.Attachment("C:\1.
$Attachment.ContentDisposi
$Attachment.ContentDisposi
$Attachment.ContentType.Me
$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To.Add($email
$MailMessage.From = "email"
$MailMessage.Subject = "Account Expiration Notice"
$MailMessage.IsBodyHtml = $True
$MailMessage.Attachments.A
$MailMessage.Body = '
<HTML xmlns:o = "urn:schemas-microsoft-com
<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@5
<v:imagedata src="--address with picture---"></v:imagedata>
<p>Hello, your account expires on $accountexpires, please change your password!</p>
<div id='BottomRight'>
<img src='CID:$($Attachment.Con
</div>
</body>
</html>'
$SmtpClient = New-Object Net.Mail.SmtpClient("mails
$SmtpClient.Send($MailMess
$Attachment.Dispose()
}
}
------------------
This is error message:
Unexpected token 'urn:schemas-microsoft-com
<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:
ASKER
thx