Repository navigation
Closed
Labels
Committee-ReviewedPS-Committee has reviewed this and made a decisionFirst-Time-IssueEasy issues first time contributors can work on to learn about this projectHacktoberfestPotential candidate to participate in HacktoberfestIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issues
Description
Not a bug, but a request for "Send-MailMessage" cmdlet
$From = $email
$To = $Number
$Body = "Something"
$Subject = $null
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $CredRESULT:
Send-MailMessage : Cannot validate argument on parameter 'Subject'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:8 char:52
+ -Body $Body -SmtpServer $SMTPServer -Subject $Subject -port $SM ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
CAUSE:
[Parameter(Mandatory=$true, Position=1)]
[Alias('sub')]
[ValidateNotNullOrEmpty()]
[string]
${Subject},
Emails do not require subjects to be present. However, in the Send-MailMessage cmdlet, the $subject parameter is set to "Mandatory=$true". Since an email does not require a subject, this should really be set to "$false".
I've created my own workaround this, but I feel that this should be changed since a subject is not imperative for an email to be sent.
Metadata
Metadata
Assignees
Labels
Committee-ReviewedPS-Committee has reviewed this and made a decisionFirst-Time-IssueEasy issues first time contributors can work on to learn about this projectHacktoberfestPotential candidate to participate in HacktoberfestIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issues
Activity
SteveL-MSFT commentedon Jun 18, 2017
Seems reasonable. Cc Joey Aiello (@joeyaiello)
iSazonov commentedon Jun 18, 2017
Missing Subject is a bad practice. By default, we must ask for it to present. Maybe add
-IgnoreSubject?ThreeFive-O commentedon Aug 20, 2018
I would like to try this one.
Most information is gathered from PR #4044, which was closed due to non-addressed comments.
The easiest way to implement the option to make
-Subjectalways mandatory, unless the user explicitly states to send the mail without a subject is to useParameterSetNameas stated by Dan Travison (@dantraMSFT).Though I would like to go with the-Forceswitch, due to consistency with other cmdlets as suggested by Steve Lee (@SteveL-MSFT).Based on community feedback (Thx for the feedback) the
-NoSubjectswitch will be used in its own parameter set. This will therefore exclude the parameter-Subjectcompletely from the proposed ParameterSetName "NoSubject".ParameterSetName "Default": This would prompt for missing parameter
-Subjectdue to being a mandatory parameterParameterSetName "Force": This would not prompt for missing parameter-SubjectParameterSetName "NoSubject": This will exclude the parameter
-Subjectfrom the parameter set. No error or prompt will be shown due to missing email subject.One problem is, that
Send-Mailuses positional parameters, where-Subjectis at position 1. Two options would be available:-Bodypositional parameter at 1 instead of 2 and-SmtpServer2 instead of 3.Example for solution 2:
TODOs
-NoSubjectswitch.-NoSubjectswitch.Any feedback is highly appreciated. Thx.
EDIT 2018-08-21
-Forceto-NoSubject.Send-Mailto correctSend-MailMessagecmdlet name.joeyaiello commentedon Aug 20, 2018
Sounds good to me. I like solution 2 👍
SteveL-MSFT commentedon Aug 20, 2018
ThreeFive-O thanks for taking this up! The one concern I have is that the proposal forces
-Forceto always apply to no-subject. Perhaps it would be better to have a-NoSubjectswitch thereby having a specific parameter set for that scenario.dantraMSFT commentedon Aug 20, 2018
Personally, I think we're overloading -Force here. Consider potential help text
'-Force: sends the mail message without a subject'.
I personally favor your other suggestion, -NoSubject.
'-NoSubject: sends the mail message without a subject.'
Changing the parameter order for the new parameter set should be fine since it requires an opt-in and won't break existing scripts.
ThreeFive-O commentedon Aug 21, 2018
Two more questions:
Strange case: Using the switch
-NoSubjectwould exclude-Subjectfrom this parameter set. What if someone uses a pipeline or splatting where both parameters-NoSubjectand-Subjectare passed through? This would result in an error, since-Subjectis not available. My other suggestion with the-Forceparameter would have allowed still to include-Subject, but to ignore the mandatory prompt.Send-MailMessageis not available in the version 6 Powershell-Docs repository. Am I missing something?iSazonov commentedon Aug 22, 2018
ThreeFive-O Please open new issue in PowerShell-Docs repo https://github.com/PowerShell/PowerShell-Docs
SteveL-MSFT commentedon Sep 5, 2018
@PowerShell/powershell-committee reviewed this, making a parameter that was mandatory no longer mandatory is not a breaking change. Recommendation is to make
mandatory=false, remove validation fornulloremptyand set default to empty string.ece-jacob-scott commentedon Feb 23, 2019
Hack Illinois Competition participant.
Working with Dan and Tyler
I can take this one on.
ece-jacob-scott commentedon Feb 23, 2019
Tested with Tyler's own smtp server.
The test when run in CI just don't run.
We used this code to test on my local environment and confirmed that it works.
ThreeFive-O commentedon Feb 24, 2019
Jacob Scott (@ece-jacob-scott) Tests for Send-MailMessage cmdlet don't run on CI, since there is no SMTP server installed on the CI agents.
See PR #8859 which would allow testing the cmdlet on the CI agents. PR is ready but yet to be merged.