PowerShell send email html image

Greetings, like-minded people. :slight_smile:
There is a script that notifies the user about the expiration of the account password.
I would like to transform it a little and add pictures to the body of the letter. But with the usual means of HTML, this cannot be done.

<img src="C:\Folder\Image.jpg"></br>

Can you please tell me how to add an image to the body of an email?

# Please Configure the following variables....
$smtpServer="smtp.domain.com"
$expireindays = 200
$from = "mail.domain.com"
$logging = "Disabled" # Set to Disabled to Disable Logging
$logFile = "<log file path>" # ie. c:\mylog.csv
$testing = "Disabled" # Set to Disabled to Email Users
$testRecipient = "testuser@company.com"
#

# 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,Notified"
    }
} # End Logging Check

# System Settings
$textEncoding = [System.Text.Encoding]::UTF8
$date = Get-Date -format ddMMyyyy
# End System Settings

# Get Users From AD who are Enabled, Passwords Expire and are Not Currently Expired
Import-Module ActiveDirectory
$users = get-aduser -filter * -SearchBase "OU=Test,DC=domain,DC=com" -properties Name, givenName, sAMAccountName, PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress | 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
    $givenName = $user.givenName
    $sAMAccountName =$user.sAMAccountName
    $emailaddress = $user.emailaddress
    $passwordSetDate = $user.PasswordLastSet
    $PasswordPol = (Get-AduserResultantPasswordPolicy $user)
    $sent = "" # Reset Sent Flag
    # 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) -gt "1")
    {
        $messageDays = "" + "$daystoexpire" + " days."
    }
    else
    {
        $messageDays = "days."
    }

    # Email Subject Set Here
    $subject="Your account password will expire in $messageDays"
    $CurrentDate = Get-Date
    $CurrentDate = $CurrentDate.ToString('dd.MM.yyy HH:mm:ss')
    $MailBody = 
@"
                                     <font size="2" color="black" face="Tahoma">Добрый день, $givenName.</br>
                                     Your account password expiration date <b>[</b> $sAMAccountName <b>]</b> expires in <b>$messageDays</b></font></br>
									 <img src="C:\Folder\Image.jpg"></br>
"@

    # 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))
    {
        $sent = "Yes"
        # If Logging is Enabled Log Details
        if (($logging) -eq "Enabled")
        {
            Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson,$sent"
        }
        # Send Email Message
        Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $MailBody -BodyAsHtml -priority High -Encoding $([System.Text.Encoding]::UTF8)

    } # End Send Message
    else # Log Non Expiring Password
    {
        $sent = "No"
        # If Logging is Enabled Log Details
        if (($logging) -eq "Enabled")
        {
            Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson,$sent"
        }
    }

}```

Straight to the point, below link should help you.

embedding image in html email - Stack Overflow

PS: I didn’t try.

1 Like