mark
mark

Reputation: 62722

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

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:


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
}


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

Invoke-Expression $expr


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.

Upvotes: 4

Views: 4601

Answers (2)

Richard
Richard

Reputation: 108975

You can use a "splat" variable to pass parameters indirectly:

$extraParams = @{}
if (-not [String]::IsNullOrEmpty($body)) {
  $extraParams['Body'] = $body
}

Send-MailMessage -To $to -Subject $subject -From $from -SmtpServer $server @extraParams

Note use of @ to pass hash table of parameter-name parameter-value pairs (you can of course pass all the parameters this way, eg. in the initialisation of $extraParams).

Upvotes: 4

Shay Levy
Shay Levy

Reputation: 126712

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

Upvotes: 5

Related Questions