Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: How to pass empty body to Send-MailMessage?

Tags:

powershell

I have this simple script:

param([string[]]$to="markk", $subject=$null, $body=$null, $from="name", $suffix="@example.com", $server="dev-builder")

function NormalizeAddress([string]$address)
{
  if ($address -notmatch "@")
  {
    $address = $address + $suffix
  }
  $address
}

if (! $subject)
{
  $subject = [DateTime]::Now
}

$from = NormalizeAddress $from
$to = $to | % { NormalizeAddress $_ }
Send-MailMessage -To $to -Subject $subject -From $from -Body $body  -SmtpServer $server

It is written that way so that one could run it without any arguments, in which case a test message would be sent to the author (me).

Currently the script fails, because passing $null in the -Body argument is not allowed:

Send-MailMessage : Impossible de valider l'argument sur le paramètre « Body ». L'argument est null ou vide. Indiquez un argument qui n'est pas null ou vide et réessayez.
Au niveau de C:\Work\hg\utils\SendEmail.ps1 : 19 Caractère : 61
+ Send-MailMessage -To $to -Subject $subject -From $from -Body <<<<  $body  -SmtpServer $server
    + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage

I have three possible solutions:

  • Two Send-MailMessage command statements:

if ($body)
{
  Send-MailMessage -To $to -Subject $subject -From $from -Body $body -SmtpServer $server
}
else
{
  Send-MailMessage -To $to -Subject $subject -From $from -SmtpServer $server
}

  • Using Invoke-Expression:

$expr = "Send-MailMessage -To `"$to`" -Subject `"$subject`" -From `"$from`" -SmtpServer `"$server`""
if ($body)
{
  $expr = "$expr -Body `"$body`""
}

Invoke-Expression $expr

  • Simulating an empty body with a single space character:

if (! $body)
{
  $body = " "
}
Send-MailMessage -To $to -Subject $subject -From $from -Body $body -SmtpServer $server

All of these solutions look bad to me, because they all have this smell of being just hacks. I must be missing something really basic here, so my question is how can I pass the empty body without having to resort to these hacks?

Thanks.

like image 577
mark Avatar asked Jan 18 '23 08:01

mark


1 Answers

The Send-MailMessage cmdlet can send emails without specifying the Body parameter. On the other hand, if you do specify a value it must not be empty or null (there's a validation attribute on the parameter, ValidateNotNullOrEmpty). So, the solution would be to create hashtable of parameter names and values (aka splatting) and pass it to the underlying cmdlet:

 param([string[]]$to="markk", $subject=$null, $body=$null, $from="name", $suffix="@example.com", $server="dev-builder")

 function NormalizeAddress([string]$address)
 {
   if ($address -notmatch "@")
   {
     $address = $address + $suffix
   }
   $address
 }

 if (! $subject)
 {
   $subject = [DateTime]::Now
 }

 $param = @{
    from=NormalizeAddress $from
    to = $to | foreach { NormalizeAddress $_ }
    subject = $subject
    smtpserver = $server    
 }

 # if body is not empty or null - add it to the hastable
 if( ![String]::IsNullOrEmpty($body) ) {$param['body']=$body}

 Send-MailMessage @param
like image 135
Shay Levy Avatar answered Jan 31 '23 01:01

Shay Levy