Skip to content

Instantly share code, notes, and snippets.

@kmhuglen
Last active August 29, 2015 14:26
Show Gist options
  • Save kmhuglen/858c4fdc92088fea8046 to your computer and use it in GitHub Desktop.
Save kmhuglen/858c4fdc92088fea8046 to your computer and use it in GitHub Desktop.
Example of how to use Send-MailMessage with conditionally CC and BCC arguments
[string]$MailFrom = "me@domain.com"
[array]$MailTo = "you@domain.com", "other@domain.com"
[string]$MailSubject = "my subject"
[string]$MailBody = @"
<b>Heading</b>
Some text<br>
New Line with more text<br>
"@
[string]$MailServer = "smtp.domain.com"
[array]$MailCc = "optional@domain.com" # Don't know if this variable is set or not
[array]$MailBcc = "optional@domain.com" # Don't know if this variable is set or not
$MailArguments = @{
From = $MailFrom
To= $MailTo
Subject = $MailSubject
Body = $MailBody
BodyAsHtml = $True
SmtpServer = $MailServer
Encoding = [System.Text.Encoding]::UTF8
}
If($MailCc){$MailArguments.Add("Cc",$MailCc)}
If($MailBcc){$MailArguments.Add("Bcc",$MailBcc)}
Send-MailMessage @MailArguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment