Skip to content

Send-MailMessage incorrectly handling ValueFromPipelineByPropertyName parameters #7591

Closed

Description

The current implementation is processing parameters in BeginProcessing. While this works for parameters that are explicitly passed to the cmdlet, it fails for parameters that are populated form properties of the pipeline object because parameters are not set until the pipeline object is sent to ProcessRecord.

The logic for parameters bound from the pipleline needs to be moved to ProcessRecord.

Steps to reproduce

$object = [PSCustomObject]@{To = $address; From = $address;  Subject = $subject;  Body = $body; SmtpServer = '127.0.0.1'}
$object | Send-MailMessage

Expected behavior

The mail is sent.

Actual behavior

System.ArgumentNullException: Value cannot be null
Parameter name: address

Activity

  1. added
    Issue-Bug
    on Aug 21, 2018
  2. ThreeFive-O commented on Sep 15, 2018

    @ThreeFive-O
    Contributor

    Send-MailMessage in its current implemented code logic only supports piping attachment file paths into the cmdlet:

    file1.txt, file2.txt | Send-MailMessage -To $address -From $address -SmtpServer 127.0.0.1 -Subject "Mail with attachments"

    In this case, the file paths file1.txt and file2.txt are added to the mail message in the ProcessRecord block of the cmdlet and the mail is sent in the EndProcessing block.

    Using named parameter pipeline binding, the mail has to be sent within the ProcessRecord block, because the pipeline may contain several input objects with named parameters:

    $objects  = [PSCustomObject]@{Subject = "First mail"; To = $address; From = $address; SmtpServer = '127.0.0.1'}, [PSCustomObject]@{Subject = "Second mail"; To = $address; From = $address; SmtpServer = '127.0.0.1'}
    $objects | Send-MailMessage

    Small problem:
    Since Attachments is the default pipeline input and it's type is string[] the named parameter objects would be bound as well to Attachments.
    For the case above, the Attachments property would still be set like this:

    Attachments[0] = "@{Subject = "First mail"; To = $address; From = $address; SmtpServer = '127.0.0.1'}"
    Attachments[1] = "@{Subject = "Second mail"; To = $address; From = $address; SmtpServer = '127.0.0.1'}"

    Since those are not valid file paths, the mail message would throw.

    Therefore the two cases have to be differentiated:

    1. Attachment file paths as pipeline input: Send a single mail message in the EndProcessing block (current behavior)
    2. Objects with named parameters as pipeline input: Send a mail message per pipeline item in ProcessRecord block (described above)
  3. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. added
    Resolution-No Activity
    on Nov 16, 2023
  6. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugResolution-No ActivityWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions