问题描述
我是一名PowerShell新手,在自动化电子邮件中嵌入图像时遇到问题。我正在使用Technet的Robert Pearman脚本的修改版本。电子邮件本身将发送给密码将在未来14天内过期的任何用户。
I am a PowerShell novice and am having issues with embedding an image in an automated email. I am using a modified version of Robert Pearman's script from Technet. The email itself is sent to any user who's password will expire in the next 14 days.
该脚本扫描帐户的AD,检索所有的详细信息并向他们发送电子邮件个人化。
The script scans AD for the accounts, retrieves all the details and sends them an email individualized to them.
将HTML中的图像嵌入到电子邮件中的所有内容都会失败。它看起来很简单,但电子邮件总是在电子邮件到达时显示红色的X HTML占位符图像。
Everything I've read on embedding the image with HTML into the email fails. It appears so simple however the email always displays the red X HTML placeholder image when the email arrives.
$smtpServer= "SMTP.company.biz"
$expireindays = 14
$from = "Helpdesk <[email protected]>"
$logging = "Disabled" # Set to Disabled to Disable Logging
$logFile = "<log file path>" # ie. c:\mylog.csv
$testing = "Enabled" # Set to Disabled to Email Users
$testRecipient = "[email protected]"
$date = Get-Date -format dd-MM-yyyy
#$images = @{image1='D:\Script_Folder\Overwatch_images\FFPasswordChange.jpg'}
$AttachmentPath = "D:\Script_Folder\Overwatch_Images\FFPasswordChange.jpg"
$Attachment = New-Object System.Net.Mail.Attachment($AttachmentPath)
$Attachment.ContentID = "image1"
# Check Logging Settings
if (($logging) -eq "Enabled")
{
# Test Log File Path
$logfilePath = (Test-Path $logFile)
if (($logFilePath) -ne "True")
{
# Create CSV File and Headers
New-Item $logfile -ItemType File
Add-Content $logfile "Date,Name,EmailAddress,DaystoExpire,ExpiresOn"
}
} # End Logging Check
# Get Users From AD who are Enabled, Passwords Expire and are Not Currently Expired
Import-Module ActiveDirectory
$users = get-aduser -filter {MemberOf -eq "Group1,OU=Groups,OU=Group Companies,DC=Company,DC=Biz"} -searchbase "OU=Users,OU=Group Companies,DC=Company,DC=Biz" -properties Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress, samaccountname | where {$_.Enabled -eq "True"} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false }
$DefaultmaxPasswordAge = (Get- ADDefaultDomainPasswordPolicy).MaxPasswordAge
# Process Each User for Password Expiry
foreach ($user in $users)
{
$Name = $user.Name
$emailaddress = $user.emailaddress
$passwordSetDate = $user.PasswordLastSet
$samaccountname = $user.samaccountname
$PasswordPol = (Get-AduserResultantPasswordPolicy $user)
# Check for Fine Grained Password
if (($PasswordPol) -ne $null)
{
$maxPasswordAge = ($PasswordPol).MaxPasswordAge
}
else
{
# No FGP set to Domain Default
$maxPasswordAge = $DefaultmaxPasswordAge
}
$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
# Set Greeting based on Number of Days to Expiry.
# Check Number of Days to Expiry
$messageDays = $daystoexpire
if (($messageDays) -ge "1")
{
$messageDays = "in " + "$daystoexpire" + " Tagen"
}
else
{
$messageDays = "heute."
}
# Email Subject Set Here
$subject="Ihr eIPN Passwort endet $messageDays"
$body = "
<HTML>
<BODY>
Sehr geehrter Comany User,
<p> Ihr eIPN Passwort für den Account $samaccountname läuft $messageDays ab.<br>
<p> Damit Sie sicher und problemlos weiterarbeiten können, bitten wir Sie Ihr Passwort schnellstmöglich zu ändern.<br>
<p> Um Ihr eIPN Passwort zu ändern, drücken Sie bitte STRG-ALT- ENTF und wählen Sie 'Kennwort ändern...' aus.<br>
<br>
<img src='CID:image1'>
<br>
<p> Sollten Sie sich im Außendienst oder nicht im Büro befinden, stellen Sie bitte vorher eine aktive VPN-Verbindung her.<br>
<p> Bei Problemen oder Fragen wenden Sie sich bitte an die Helpdesk unter der +49 9999 999999.<br>
<p> Mit freundlichen Grüßen, <br>
Deutschland Helpdesk <br>
</P>
</body>
</HTML>"
# If Testing Is Enabled - Email Administrator
if (($testing) -eq "Enabled")
{
$emailaddress = $testRecipient
} # End Testing
# If a user has no email address listed
if (($emailaddress) -eq $null)
{
$emailaddress = $testRecipient
}# End No Valid Email
# Send Email Message
if (($daystoexpire -ge "0") -and ($daystoexpire -lt $expireindays))
{
# If Logging is Enabled Log Details
if (($logging) -eq "Enabled")
{
Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson"
}
# Set Parameters
$params = @{
#InlineAttachements = $Images
#Attachments = 'D:\Script_Folder\Overwatch- Images\FFPasswordChange.jpg'
Body = $body
BodyasHTML = $true
Subject = $subject
From = $from
To = $emailaddress
SMTPServer = $smtpServer
}
# Send Email Message
Send-Mailmessage @params -Priority High
# -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -InlineAttachments $images -priority High
} # End Send Message
} # End User Processing
# End
我还是我的一些早期尝试在代码中进行了扩展。我觉得我是在正确的路径,但它从来没有执行正确。
I still have some of my earlier attempts hashed out within the code. I feel like I was on the right path but it never executed correctly.
如果有人可以看到我错了什么或指向正确的方向,那将是非常感谢!
If anyone can see where I am going wrong or point me in the right direction, that would be most appreciated!
推荐答案
如果您有PowerShell v4,那么这将使用splatting和 Send-MailMessage
。
If you have PowerShell v4 then this would work using splatting and Send-MailMessage
.
$attachment = Get-Item C:\temp\WhiteLogo.png
$params = @{
To = "address"
From = "address"
SMTPServer = "theIP"
Subject = "Stuff"
BodyAsHTML = $true
Body = 'Testing Inline <br /><img src="{0}" />' -f ($attachment.Name)
Attachments = $attachment.FullName
}
Send-MailMessage @params
逻辑似乎已经改进了引用v4中的内联图像。
The logic appears to have improved for referencing inline images in v4.
这篇关于无法使用Send-Mailmessage在电子邮件中嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!