Embed images in email and send via Powershell

后端 未结 1 1125
误落风尘
误落风尘 2021-01-14 13:17

I am editing one of my old scripts to send an email to a user with images embedded into the text. I am trying to use the Send-MailMessage function to send the email as oppos

相关标签:
1条回答
  • 2021-01-14 13:34

    So the real question is how to embed a image into the HTML document from a attachment

    CID aka Content ID will allow you to attach a image and then use that attached image in the document. Avoid using spaces in the Content ID name.

    Send-MailMessage -To "Test@Test.com" -from "Test2@Test.com" -SmtpServer SMTP.TEST.NET -Subject "Hello" -BodyAsHtml -Body "<img src='cid:Test.png'>" -Port 25 -Attachments "C:\Users\Test\Test.png"
    

    You are using

    $att1 = new-object Net.Mail.Attachment ("T:\PS Scripts\Images\shareddrive1.png")
    $att2 = new-object Net.Mail.Attachment ("T:\PS Scripts\Images\shareddrive2.png")
    $att3 = new-object Net.Mail.Attachment ("T:\PS Scripts\Images\shareddrive3.png")
    $att4 = new-object Net.Mail.Attachment ("T:\PS Scripts\Images\shareddrive4.png")
    $att1.ContentId = "image1.png"
    $att2.ContentId = "image2.png"
    $att3.ContentId = "image3.png"
    $att4.ContentId = "image4.png"
    

    but when you send the mail you are not attaching those attachments

    Send-MailMessage -To "<$UserID>" -bcc "<$Username@dana.com>" -from "<itservicedesk@x.com>" -Subject $global:subject -SmtpServer "mailrelay.x.com" -BodyAsHtml -body $global:body
    

    You could stop using the Net.Mail.Attachment and instead do something like

    $Body = @"
        <html>
            <body style="font-family:calibri"> 
                <b>This is image 1</b>
                <img src='cid:TEST1.png'>
                <b>This is image 2</b>
                <img src='cid:Test2.png'>
            </body>
        </html>
    "@
    
    Send-MailMessage -To "Test@Test.com" `
        -from "Test2@Test.com" `
        -SmtpServer Test.smtp.com `
        -Subject "Hello" `
        -BodyAsHtml -body $body `
        -Attachments "C:\Test\TEST1.png", "C:\Test\TEST2.png"
    
    0 讨论(0)
提交回复
热议问题