Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 将图像嵌入电子邮件并通过Powershell发送_Html_Powershell - Fatal编程技术网

Html 将图像嵌入电子邮件并通过Powershell发送

Html 将图像嵌入电子邮件并通过Powershell发送,html,powershell,Html,Powershell,我正在编辑我的一个旧脚本,以便向用户发送一封包含嵌入文本的图像的电子邮件。我正在尝试使用Send MailMessage功能来发送电子邮件,而不是以前的$smtp.Send($msg)方法。但是,当尝试更新脚本时,图像不再被嵌入 我知道如何将它们作为实际附件附加到电子邮件中,但我不确定将它们显示为实际嵌入的图像有什么不对 注意:为了简洁起见,我删除了一些完整的电子邮件,因为它很大,只要我能得到一两张图片,它们都能工作 # force powershell to run as an x86 pro

我正在编辑我的一个旧脚本,以便向用户发送一封包含嵌入文本的图像的电子邮件。我正在尝试使用Send MailMessage功能来发送电子邮件,而不是以前的$smtp.Send($msg)方法。但是,当尝试更新脚本时,图像不再被嵌入

我知道如何将它们作为实际附件附加到电子邮件中,但我不确定将它们显示为实际嵌入的图像有什么不对

注意:为了简洁起见,我删除了一些完整的电子邮件,因为它很大,只要我能得到一两张图片,它们都能工作

# force powershell to run as an x86 process
    Set-ExecutionPolicy -Scope CurrentUser Unrestricted
    if ($env:Processor_Architecture -ne "x86") {
        &"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -file $myinvocation.Mycommand.path
        exit
    }

# initialize the script
    if ($startupvariables) { try {Remove-Variable -Name startupvariables  -Scope Global -ErrorAction SilentlyContinue } catch { } }
    New-Variable -force -name startupVariables -value ( Get-Variable | ForEach-Object { $_.Name } )
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
    $Separator = ".", "@"

# advise what the script does
    Add-Type -AssemblyName PresentationCore,PresentationFramework
    $ButtonType = [System.Windows.MessageBoxButton]::OKCancel
    $MessageIcon = [System.Windows.MessageBoxImage]::Warning
    $MessageTitle = "Shared Drive Access Assistance"
    $MessageBody = "This script asks the user to provide more information regarding a network drive that they would like access to.`n`nTo use it, enter the below information:`n`n`n`tTicket Number`n`n`tUser's Email Address`n`n`tRequestor's Email Address`n`n`nIf this is the script you want to use, click OK.`nIf not, click Cancel."
    $Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

    if ($Result -eq "Cancel")
    {
    Exit-PSSession
    }
    else
    {

# get the ticket number
    $Ticket = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the SCTask ticket number" , "Ticket Number")

# get the user id via the email address
    $UserID = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the user's email address" , "User Email Address")
    $User = $UserID.split($Separator)
    $Firstname = $User[0].substring(0,1).toupper()+$User[0].substring(1).tolower()
    $Lastname = $User[1].substring(0,1).toupper()+$User[1].substring(1).tolower()
    $User = $Firstname, $Lastname

# get local username
    $Username = [System.Environment]::UserName

# create email
    $subject = "Ticket $Ticket on Hold for User Response - Shared Drive Access for $User - Awaiting Additional Information"
    $body = @"
    <html>
    <body style="font-family:calibri"> 
    To $Requestor, $User,<br>
    <br>
    <br>
    In order to proceed with your request for shared drive access, we require the server name and full path to the folder you need access to. If you do not already know this information, you will need to provide these instructions to someone that already has access to the folder that you need access to.<br>
    <br>
    1)  Click the Start menu<br>
    <br>
    <img src="cid:image1.png"><br>
    <img src="cid:image2.png"><br>
    <img src="cid:image3.png"><br>
    <br>
    <br>
    2)  Navigate to "Computer"<br>
    <br>
    <img src="cid:image4.png"><br>
    <br>
    <br>

    <br>
    If you have any questions or need assistance with this process, please contact the Service Desk via one of the methods listed below. 
    <br>
    <br>
    Thank You,<br>
    <br>
    IT Service Desk<br>

    </body>
    </html>
"@

    $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"
#    $msg.Attachments.Add($att1)
#    $msg.Attachments.Add($att2)
#    $msg.Attachments.Add($att3)
#    $msg.Attachments.Add($att4)

# create confirmation message
    $ButtonType = [System.Windows.MessageBoxButton]::YesNo
    $MessageIcon = [System.Windows.MessageBoxImage]::Warning
    $MessageTitle = "Shared Drive Access Assistance"
    $MessageBody = "The information you have entered is show below:`n`n`nTicket Number: $Ticket`n`nUser's Email Address: $UserID`n`nRequstor's Email Address: $RequestorID`n`n`nIf you would like to send the email, click Yes.`nOtherwise, click No."
    $Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
    if ($Result -eq "No")
    {
    Exit-PSSession
    }
    else

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

Function Clean-Memory {
    Get-Variable |
        Where-Object { $startupVariables -notcontains $_.Name } |
            ForEach-Object {
            try { Remove-Variable -Name "$($_.Name)" -Force -Scope "global" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue}
                catch { }
 }
 }
#强制powershell作为x86进程运行
设置ExecutionPolicy-作用域CurrentUser不受限制
if($env:处理器体系结构-ne“x86”){
&“$env:windir\syswow64\windowspowershell\v1.0\powershell.exe”-文件$myinvocation.Mycommand.path
出口
}
#初始化脚本
if($startupvariables){try{Remove Variable-Name startupvariables-Scope Global-ErrorAction SilentlyContinue}catch{}
新变量-force-name startupVariables-value(获取变量| ForEach对象{$\u.name})
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')| Out Null
$Separator=“.”,“@”
#建议脚本的作用
添加类型-AssemblyName PresentationCore、PresentationFramework
$ButtonType=[System.Windows.MessageBoxButton]::确定取消
$MessageIcon=[System.Windows.MessageBoxImage]::警告
$MessageTitle=“共享驱动器访问帮助”
$MessageBody=“此脚本要求用户提供有关他们希望访问的网络驱动器的详细信息。`n`n`n`t输入以下信息:`n`n`t邮箱号码`n`n`t用户的电子邮件地址`n`n`tRequestor的电子邮件地址`n`n`n`n`n如果这是您要使用的脚本,请单击“确定”。`n`n`t如果不是,请单击“取消”
$Result=[System.Windows.MessageBox]::显示($MessageBody、$MessageTitle、$ButtonType、$MessageIcon)
如果($结果-等式“取消”)
{
退出会话
}
其他的
{
#拿到车票号码
$Ticket=[Microsoft.VisualBasic.Interaction]::InputBox(“输入SCTask票证号”,“票证号”)
#通过电子邮件地址获取用户id
$UserID=[Microsoft.VisualBasic.Interaction]::InputBox(“输入用户的电子邮件地址”,“用户电子邮件地址”)
$User=$UserID.split($Separator)
$Firstname=$User[0]。子字符串(0,1).toupper()+$User[0]。子字符串(1).tolower()
$Lastname=$User[1]。子字符串(0,1).toupper()+$User[1]。子字符串(1).tolower()
$User=$Firstname,$Lastname
#获取本地用户名
$Username=[System.Environment]::Username
#创建电子邮件
$subject=“Ticket$Ticket等待用户响应-$User共享驱动器访问-等待其他信息”
$body=@“
到$Requestor,$User,


为了继续您的共享驱动器访问请求,我们需要您需要访问的文件夹的服务器名称和完整路径。如果您还不知道这些信息,您需要向已经有权访问您需要访问的文件夹的人提供这些说明。

1) 单击开始菜单






2) 导航到“计算机”





如果您对此流程有任何疑问或需要帮助,请通过下列方法之一与服务台联系。

谢谢你,

IT服务台
"@ $att1=新对象Net.Mail.Attachment(“T:\PS Scripts\Images\shareddrive1.png”) $att2=新对象Net.Mail.Attachment(“T:\PS Scripts\Images\shareddrive2.png”) $att3=新对象Net.Mail.Attachment(“T:\PS Scripts\Images\shareddrive3.png”) $att4=新对象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” #$msg.Attachments.Add($att1) #$msg.Attachments.Add($att2) #$msg.Attachments.Add($att3) #$msg.Attachments.Add($att4) #创建确认消息 $ButtonType=[System.Windows.MessageBoxButton]::是否 $MessageIcon=[System.Windows.MessageBoxImage]::警告 $MessageTitle=“共享驱动器访问帮助” $MessageBody=“您输入的信息如下所示:`n`n`n支票号码:$Ticket`n`n`n用户的电子邮件地址:$UserID`n`nRequestor的电子邮件地址:$RequestorID`n`n`n`如果您想发送电子邮件,请单击“是”。`n否则,请单击“否” $Result=[System.Windows.MessageBox]::显示($MessageBody、$MessageTitle、$ButtonType、$MessageIcon) 如果($结果-等式“否”) { 退出会话 } 其他的 #发送电子邮件 { 发送邮件-至“-bcc”-从“-Subject$global:Subject-SmtpServer”mailrelay.x.com”-BodyAsHtml-body$global:body } } 功能清除存储器{ 获取变量| 其中,对象{$startupVariables-notcontains$\名称}| ForEach对象{ 请尝试{Remove Variable-Name“$($.Name)”-Force-Scope“global”-ErrorAction SilentlyContinue-WarningAction SilentlyContinue} 捕获{} } }
所以真正的问题是如何将图像从附件嵌入HTML文档中

CID aka Content ID允许您附加图像,然后在文档中使用该附加图像。避免在内容ID名称中使用空格

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"
但是,当您发送邮件时,您没有附加这些附件

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

您可以将图像嵌入HTML,因此没有额外的文件。 它正在取代

{img=file}
使用数据URL

{src="data:image/png;base64,[base64 encoded long string representing the image]}
有许多在线转换器可以将图像文件转换为所需代码


只需在谷歌上搜索图像到数据的URI转换器。

我遇到了与您相同的问题,我最终使用了Send InlineMailMessage模块。请看一下
{img=file}
{src="data:image/png;base64,[base64 encoded long string representing the image]}