PowerShell Automation - How To Send Email With HTML Body Containing Inline Images

During a recent assignment I got an automation requirement to send formatted emails daily to a set of users.

The email content involved the HTML formatting with a lot of images as a part of the layout. I have appointed PowerShell as the automation technology to get this done.

I felt it would be worth writing a small article to explain the code (which is interesting) to embed images within the outgoing email content.

Now to start with a demo, say we have to send an email with Health of Weekly Sales Card for an online shopping store every week on Monday.

This email would look like the one in the following screenshot,

 

SharePoint

In Steps 1, 2, 4 we are defining HTML that will be sent as Email Content.

 Here it is very important to notice the “SRC” Tag where we are specifying image path not as an actual path but as a Content Identifier that is mapped to the path of the attachment added to the email.

And this is the trick that you have to remember to get the images embedded into the HTML body of the email.

 

SharePoint

SharePoint

SharePoint

 

Now, once we are done with defining body content for Email Body, we can start configuring SMTP Client and adding the attachments to the email.

In Step 5 we will initialize “SMTP Server”, “Mail Message”, and “SMTP Client” Objects

Step 6 configure “From” & “To” Email IDs for the intended recipients. Also, specify the “Subject” & “Body” for the email

 

SharePoint

 

In Step 7 & 8 we will add attachments to the email and provide each attachment a Content Identifier (remember we used this identifier in “SRC” Tag while defining HTML content for the body).

Attachments can be added to the email by using object of “Attachment” Class.

While initializing the “Attachment” Object we have to provide an actual path of the attachment as an input parameter

Then we have to make sure that

  1. “Inline” property should be set to “True”,
  2. “DispositionType” property should be set to “Inline”,
  3. “MediaType” property should be set to “<type of content, you are attaching>”,
  4. “ContentId” property should be set to any unique Content Identifier to represent the attachment uniquely

 

SharePoint
SharePoint

 

In Step 9 we will send the email by calling in “Send” method of SMTP Client object. Make sure to dispose of the “Attachments” & “Mail Message” objects.

 

SharePoint

 

And finally, we will call the Send-Email Function in Step 10 to run all the code snippets described earlier.

 

SharePoint

 

Code Snippet 
  1. <#$body = @"  
  2. <html>  
  3. <body>  
  4. <img src="cid:Arrow.png">  
  5. <img src="cid:Arrows-2.jpg">  
  6. <img src="cid:Arrows-3.jpg">  
  7. </body>  
  8. </html>  
  9. "@#>  
  10.   
  11. <#<table width='500px;' style='padding:10px;padding-right:20px;padding-left:20px;'>  
  12.      <tr>  
  13.      <td colspan='6'>  
  14.         <strong style='color:#FFFFFF;background:#045FB4;'>Header</strong>     
  15.      </td>  
  16.      </tr>  
  17.      </table>  
  18.      <strong style='background: #045fb4; text-align: center; color: #ffffff;'><img id='mfImage' style='display: block;' src='cid:logo.jpg' alt='' />Title </strong>  
  19.        
  20.   
  21.      <table style='padding:10px;padding-right:20px;padding-left:20px;'>  
  22.      #>  
  23.   
  24. $body = @"  
  25.        
  26.      <table style='padding:5px;'>  
  27.         <tbody>  
  28.         <tr style='background: #848484; text-align: center; color: #ffffff;'>  
  29.                 <td colspan='2' style='width: 10%;background: #FFFFFF;'><img id='mfImage' style='display: block;' src='cid:logo.jpg' alt='' /></td>  
  30.                 <td style='width: 70%;' colspan='3'>  
  31.                     <h3>Computer Operations Daily Status</h3>  
  32.                 </td>  
  33.                 <td style='width: 20%;' colspan='1'><h3>Date: 05/06/2017</h3></td>  
  34.             </tr>  
  35.             <tr style='color:#FFFFFF;background:#848484; border-right:1px solid;border-left:1px solid;border-bottom:1px solid;'>  
  36.                 <td align='center' style='width: 50px;'>  
  37.                     <h4>Mainframe</h4>  
  38.                 </td>  
  39.                 <td align='center' style='width: 50px;'>  
  40.                     <h4>Batch</h4>  
  41.                 </td>  
  42.                 <td align='center' style='width: 50px;'>  
  43.                     <h4>Network</h4>  
  44.                 </td>  
  45.                <td align='center' style='width: 50px;'>  
  46.                     <h4>Servers</h4>  
  47.                 </td>  
  48.                 <td align='center' style='width: 50px;'>  
  49.                     <h4>Applications</h4>  
  50.                 </td>  
  51.                 <td align='center' style='width: 50px;'>  
  52.                     <h4>Other</h4>  
  53.                 </td>  
  54.             </tr>  
  55.             <tr style='border-right:1px solid;border-left:1px solid;border-bottom:1px solid;'>  
  56.             <td align='center' style='border:1px solid;'>  
  57.                 <p><img id='mfImage' src='cid:lowIssues.png' style='display:block;' alt='' /></p>  
  58.              </td>  
  59.               <td align='center' style='border:1px solid;'>  
  60.                 <p><img id='batImage' src='cid:moderateIssues.jpg' alt='' /></p>  
  61.              </td>  
  62.               <td align='center' style='border:1px solid;'>  
  63.                 <p><img id='netImage' src='cid:severeIssues.jpg' alt='' /></p>  
  64.              </td>  
  65.               <td align='center' style='border:1px solid;'>  
  66.                 <p><img id='serverImage' src='cid:lowIssues.png' alt='' /></p>  
  67.              </td>  
  68.               <td align='center' style='border:1px solid;'>  
  69.                 <p><img id='appImage' src='cid:moderateIssues.jpg' alt='' /></p>  
  70.              </td>  
  71.               <td align='center' style='border:1px solid;'>  
  72.                 <p><img id='otherImage' src='cid:severeIssues.jpg' alt='' /></p>  
  73.              </td>  
  74.             </tr>  
  75.         </tbody>  
  76.     </table>  
  77.     <br/><br/>  
  78.   
  79.     <table>  
  80.         <tbody>  
  81.             <tr style='height: 30px; border: 1px solid; color: #ffffff; background: #848484;'>  
  82.                 <td style='width: 265px; border: 1px solid;padding:5px;' colspan='4'><b>IT Business manager of the week</b></td>  
  83.             </tr>  
  84.             <tr style='height: 30px; border: 1px solid;'>  
  85.                 <td style='width: 15%; border: 1px solid; color: #ffffff; background: #848484;padding:5px;'>Primary:</td>  
  86.                 <td style='width: 35%; border: 1px solid;padding:5px;'>Prashant Bansal</td>  
  87.                 <td style='width: 15%; border: 1px solid; color: #ffffff; background: #848484;padding:5px;'>Email:</td>  
  88.                 <td style='width: 35%; border: 1px solid;padding:5px;'>Email Ids</td>  
  89.             </tr>  
  90.             <tr style='height: 30px;'>  
  91.                 <td style='width: 15%; border: 1px solid; color: #ffffff; background: #848484;padding:5px;'>Backup:</td>  
  92.                 <td style='width: 35%; border: 1px solid;padding:5px;'>Bansal Prashant</td>  
  93.                 <td style='width: 15%; border: 1px solid; color: #ffffff; background: #848484;padding:5px;'>Email:</td>  
  94.                 <td style='width: 35%; border: 1px solid;padding:5px;'>Email IDs</td>  
  95.             </tr>  
  96.         </tbody>  
  97.     </table>  
  98.     <br/><br/>  
  99.   
  100.     <p>Daily Status has been posted. Please use the following link.<br/><a href="Target URL">Link Title</a></p>  
  101.     <p></p>  
  102.   
  103. "@  
  104.   
  105. function Send-EMail()  
  106. {  
  107.     $smtpServer = "<SMTP Server Name>"  
  108.     $msg = new-object Net.Mail.MailMessage  
  109.     $smtp = new-object Net.Mail.SmtpClient($smtpServer)  
  110.   
  111.     $msg.From = "<From Email Id>"  
  112.     $msg.To.Add("<To Email Ids>")  
  113.     $msg.subject = "This is an email with inline images"  
  114.   
  115.     $msg.IsBodyHtml = $True  
  116.   
  117.     $msg.Body = $body  
  118.   
  119.     $attachment = New-Object System.Net.Mail.Attachment –ArgumentList "lowIssues.png"  
  120.     $attachment.ContentDisposition.Inline = $True  
  121.     $attachment.ContentDisposition.DispositionType = "Inline"  
  122.     $attachment.ContentType.MediaType = "image/png"  
  123.     $attachment.ContentId = 'lowIssues.png'  
  124.     $msg.Attachments.Add($attachment)  
  125.   
  126.     $attachment = New-Object System.Net.Mail.Attachment –ArgumentList "moderateIssues.jpg"  
  127.     $attachment.ContentDisposition.Inline = $True  
  128.     $attachment.ContentDisposition.DispositionType = "Inline"  
  129.     $attachment.ContentType.MediaType = "image/jpg"  
  130.     $attachment.ContentId = 'moderateIssues.jpg'  
  131.     $msg.Attachments.Add($attachment)  
  132.   
  133.     $attachment = New-Object System.Net.Mail.Attachment –ArgumentList "severeIssues.jpg"  
  134.     $attachment.ContentDisposition.Inline = $True  
  135.     $attachment.ContentDisposition.DispositionType = "Inline"  
  136.     $attachment.ContentType.MediaType = "image/jpg"  
  137.     $attachment.ContentId = 'severeIssues.jpg'  
  138.     $msg.Attachments.Add($attachment)  
  139.   
  140.     $attachment = New-Object System.Net.Mail.Attachment –ArgumentList "logo.jpg"  
  141.     $attachment.ContentDisposition.Inline = $True  
  142.     $attachment.ContentDisposition.DispositionType = "Inline"  
  143.     $attachment.ContentType.MediaType = "image/jpg"  
  144.     $attachment.ContentId = 'logo.jpg'  
  145.     $msg.Attachments.Add($attachment)  
  146.   
  147.     $smtp.Send($msg)  
  148.     $attachment.Dispose();  
  149.     $msg.Dispose();  
  150.   
  151. }  
  152.   
  153. Clear-Host  
  154. Send-EMail  

That is all for this demo.

I hope you find it helpful.


Similar Articles