0

Does anyone have an idea on how to fix this issue. I'm working on automating new user creation and notification in Active Directory (AD). I have written a PS script to import & create new users from a CSV file in PowerShell and I will like to communicate the "userid" and password to each of the users using PS Send-MailMessage option.

Below script handles this fine, but the format is not looking good, as you can see on the attachment.

Actually, there are two issues, that I'm trying to resolve.

  1. I keep getting the smtp pop to provide my smtp email password for each user on the csv. enter image description here

  2. The format of the email, as you can see on the email format is not looking good. enter image description here

I have attached a screenshot on, how I'm hoping to get the email formatted.enter image description here

$SupportEmail = "no-reply@exampledomain.com"
$To = "$EmailAddress"
$Cc = " no-reply@exampledomain.com""
$Attachment = "C:\Accountsetup.pdf"
$subject = "Welcome - Account Setup Credentials 
$SMTPServer = "smtp.gmail.com"
$SMTPPort ="587"


 $body = "Hello $DisplayName," 

 $body += "This e-mail contains your user name and first-time password         for signing 
into our web service"

$body += "Acount UserName: $SamAccountName@exampledomain.com"
$body += "Temporary Password: $Password"
$body += "Please see the attached userguide on how to reset your   password using your 
temporary assigned password "
$Creds = (Get-Credential -Credential "$SupportEmail")
Start-Sleep 2
Send-MailMessage -From $SupportEmail -to $To -cc $Cc -Attachment       

$Attachment -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl `
-Credential $Creds -DeliveryNotificationOption never
1
  • 1
    I could maybe move to stackoverflow if you need, but it's your $body. As is it's text only, but you need to send HTML encoding, which mean you need to construct your email layout kinda like a webpage, and after you paste all the correct html encoding you need.IE; $body += "Click <a href=google.com>here</a> to open google <br>" – yagmoth555 Nov 1 '19 at 2:04
1

I've found this post on Stackoverflow for passing credentials to Send-MailMessage

Nick answered Sep 17 '12 at 16:01

I love to make functions for things, and I need all the practice I can get, I went ahead and wrote this:

Function Send-EMail {
    Param (
        [Parameter(`
            Mandatory=$true)]
        [String]$EmailTo,
        [Parameter(`
            Mandatory=$true)]
        [String]$Subject,
        [Parameter(`
            Mandatory=$true)]
        [String]$Body,
        [Parameter(`
            Mandatory=$true)]
        [String]$EmailFrom="myself@gmail.com",  #This gives a default value to the $EmailFrom command
        [Parameter(`
            mandatory=$false)]
        [String]$attachment,
        [Parameter(`
            mandatory=$true)]
        [String]$Password
    )

        $SMTPServer = "smtp.gmail.com" 
        $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
        if ($attachment -ne $null) {
            $SMTPattachment = New-Object System.Net.Mail.Attachment($attachment)
            $SMTPMessage.Attachments.Add($SMTPattachment)
        }
        $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
        $SMTPClient.EnableSsl = $true 
        $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom.Split("@")[0], $Password); 
        $SMTPClient.Send($SMTPMessage)
        Remove-Variable -Name SMTPClient
        Remove-Variable -Name Password

} #End Function Send-EMail

To call it, just use this command:

Send-EMail -EmailTo "Myself@gmail.com" -Body "Test Body" -Subject "Test Subject" -attachment "C:\cdf.pdf" -password "Passowrd"

Also, for your second part about the formatting, put <br /> tags at the ends of your lines instead of starting a new line.

The formatting you use won't send new lines when you send a second $body attribute and you need to specifically declare a <br /> at the end for a newline to appear in an email, as it uses HTML formatting.

If you're using the function I posted, this will therefore go after the -Body tag

-Body "Hello $DisplayName, This e-mail contains your user name and first-time password         for signing 
into our web service <br /> Acount UserName: $SamAccountName@exampledomain.com <br /> Temporary Password: $Password <br /> Please see the attached userguide on how to reset your   password using your 
temporary assigned password "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.