Powershell - Send-MailMessage - not sending to a variable -To recepient ($MailRecepient = $SamAccountName + "@abc.ie")

Nelson Figueroa 31 Reputation points
2022-11-11T15:24:38.597+00:00

All part of the script is working except the addressee in the "-To" portion is not getting the email. Below is the script:

Script for new-aduser here…then email user

$SamAccountName = "jlazy"
$Name = "John Lazy"
$MailRecepient = $SamAccountName + "@jaswant .ie" <<<<<<<<<<<<THIS
$mailsubject = "Domain Account Created - " + $Name
$mailBody = "Dear " + $Name + ",rnrn"
$mailBody += "Your domain account has been created.rnrn"
$mailBody += " username: " + $SamAccountName + "rn"
$mailBody += " password: password1234 rnrn"
$mailBody += "Please change your password right away at any computer in the hospital by logging in or by pressing Ctrl + Alt + Delete on the keyboard.rnrn"
$mailBody += "Your IT Department"

Send-MailMessage -From 'Account Creator<******@abc.ie>'
-To $MailRecepient <<<<<<<<<<<<<<<THIS -Cc '******@abc.ie'
-Subject $mailsubject -Body $mailBody
-SmtpServer "192.168.1.7" `
-Port "587"

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-11-11T16:52:19.613+00:00

    You've misspelled "$MailRecipient" as "$MailRecepient" in the value supplied for the "-To" parameter.

    When you run into problems you don't understand, try adding Set-PSDebug -Strict at the top of the script and then run the code. Or, better, use an IDE like VS Code (with the PowerShell Pro Tools extension), or the free version of Visual Studio for your development work. They include tools that will point out errors like that before you run the code.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-11-11T16:04:48.853+00:00

    Is it safe to assume that the SMTP address "no-reply@jaswant .ie" exists? If it does, is there a Non-Delivery Report (NDR) in the mailbox?

    If that address doesn't accept e-mail, check the SMTP protocol log on the SMTP server at the IP address 192.168.1.7.

    Are you getting an error message when the Send-MailMessage runs? If not, the message was accepted for delivery by the SMTP server. It's now the responsibility of the SMTP server to either deliver the message to the recipient, pass the message to another server (i.e., relay the message), or to send a NDR to the sender of the message.

    0 comments No comments

  2. Nelson Figueroa 31 Reputation points
    2022-11-11T16:35:58.907+00:00

    The Send-MailMessage works, that is it is sending emails to the -Cc addressee 'helpdesk@jaswant .ie'. So all the technicalities you mentioned are not the problem.

    The problem is the "-To" addressee is not receiving email.

    I generate the email address by $MailRecepient = $SamAccountName + "@jaswant .ie". Then plug the $MailRecepient to the -To argument like so "-To $MailRecepient". I need to supply a none static email address.

    If I replaced the "-To $MailRecepient" with "-To 'someone@jaswant .ie'", that 'someone@jaswant .ie' will receive the email.

    Is variable like $MailRecepient allowed in the "-To" argument?

    0 comments No comments

  3. Nelson Figueroa 31 Reputation points
    2022-11-14T12:09:03.21+00:00

    Thanks for your help, I certainly learned from your comments.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.