Updated February 28, 2023
Introduction to PowerShell Send Mail
To send an email message from PowerShell the Send-MailMessage Cmdlet is used. Many times, the administrator may require a script to run to generate some reports like when the last reboot time was, how many times the server was down in the last 24 hours, etc. In this scenario, an email with the report must be sent to the administrator. For this purpose, Send-MailMessage cmdlet is used. The main parameter of this cmdlet is the SMTP server. There must be a valid SMTP server configured in the environment to be able to send and receive emails. In case if there is no SMTP server mentioned, PowerShell will take the variable set in the $PSEmailServer variable.
Syntax and Parameters
Below we will see the syntax and parameters
Syntax
Below is the syntax
Code:
NAME
Send-MailMessage
SYNTAX
Send-MailMessage [-To] <string[]> [-Subject] <string> [[-Body] <string>] [[-SmtpServer] <string>] -From <string> [-Attachments <string[]>] [-Bcc <string[]>]
[-BodyAsHtml] [-Encoding <Encoding>] [-Cc <string[]>] [-DeliveryNotificationOption {None | OnSuccess | OnFailure | Delay | Never}] [-Priority {Normal | Low | High}]
[-Credential <pscredential>] [-UseSsl] [-Port <int>] [<CommonParameters>]
ALIASES
Parameters
Below are the parameters:
- Attachments: This parameter denotes the files that need to be added as attachments in the email to be sent. Its type is a string. Its alias is PSPath. The default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
- Bcc: This denotes the email addresses of the recipients who will get the email but won’t be marked directly. Its type is a string. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
- Body: This denotes the main body of the email. Its type is a string. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
- BodyAsHtml: This denotes the body of the message to be converted as HTML. Its type is the switch parameter. Its alias is BAH. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
- CC: It denotes the recipients who should receive the carbon copy of this message. Its type is a string. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
- Credential: This denotes the credentials that should be used for the send-mailmessage cmdlet. By default, current user credentials are taken. Its type is PSCredential. Its default value is the current user. It accepts pipeline input but doesn’t accept wildcard characters.
- DeliveryNotificationOption: This denotes the notification status of the email sent. It can be multiple values. Its alias is DNO. Its default value is none. The address specified will receive the notification. It accepts pipeline input but doesn’t accept wildcard characters. The allowed values are
- None: This means no notification is sent.
- OnSuccess: This denotes a notification is sent if the mail is delivered successfully.
- Onfailure: This sends a notification if the mail is not delivered.
- Delay: This sends a notification if there is a delay in sending the email.
- Never: This denotes any notification.
Encoding: This denotes the encoding type. The acceptable values are as follows:
- ASCII
- OEM
- Unicode
- UTF7
- UTF8
- UTF8BOM
- UTF8NoBOM
- UTF32
Its type is encoding, and the alias is BE. The default value is UTF8NoBOM.It accepts pipeline input but doesn’t accept wildcard characters.
- From: This is a mandatory parameter and denotes the address that should be in the email. This denotes the sender. Its type is a string. Its default value is the current user. It accepts pipeline input but doesn’t accept wildcard characters.
- Port: Denotes the port number of the SMTP Server. By default, it’s 25. Its type is Int32. It accepts pipeline input but doesn’t accept wildcard characters.
- Priority: This denotes the priority of the email. The default value is normal. The other acceptable values are high and low. Its type is MailPriority.It accepts pipeline input but doesn’t accept wildcard characters.
- ReplyTo: This denotes the email addresses to which the receiver can reply other than the from address. Its type is a string and default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
- SMTPServer: This denotes the SMTP Server. The default value is the value present in the $PSEmailServer environment variable. Its type is string and alias is ComputerName.It accepts pipeline input but doesn’t accept wildcard characters.
- Subject: This denotes the subject of the email. This is a mandatory parameter. Its type is a string. Its alias is sub. The default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
- To: This denotes the recipient’s address. This is a mandatory parameter. Multiple addresses are allowed, and they are separated by “;”. Its type is a string and default value is none. It accepts pipeline input but doesn’t accept wildcard characters. It accepts pipeline input but doesn’t accept wildcard characters.
- UseSsl: This is used for the secure transmission of email. Its type is the switch parameter. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
Example to Implement PowerShell Send Mail
In the below script, kindly modify the from address($from), to address($to) and smtp server($SmtpServer) and attachment path($testattachment) with your appropriate values.
Code:
Write-Host "Welcome to the demo of sending email in PowerShell"
$from="[email protected]"
Write-Host "From address will be" $from
$to="************"
$subject="This is an example of test mail from PowerShell"
Write-Host "Subject of the email is" $subject
$body="This is a test email. This is sent from powershell using Send-MailMessage Cmdlet"
Write-Host "The body of the email will be" $body
$SmtpServer = "***********"
$html=""
$actualbody = "<html>
<style>
BODY{font-family: Arial; font-size: 12pt;}
H1{font-size: 26px;}
H2{font-size: 24px;}
H3{font-size: 08px;}
</style>
<body>
<h1 align=""center"">Test Email</h1>
<h3 align=""center"">$body</h3>
<p>Welcome</p>
<p>Test email</p><br/>" + $html
$testattachment="C:\Users \Desktop\Articles\Mar\PowerShell Wild cards.docx"
Send-MailMessage -From $from -to $to -Subject $subject -SmtpServer $SmtpServer -Body $actualbody -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8)
Write-Host "Email Sent"
Output:
Below is how the received email is looked like.
Conclusion
Thus, the article covered in detail about how an email is sent from PowerShell with the help of Send-MailMessage Cmdlet. It explained the different parameters that are required, their type and showed an example script of how to send an email. The most important parameter that is required is the SMTP server unless an SMTP server is configured email won’t be sent. The way to learn more about the cmdlet would be explored by writing sample scripts.
Recommended Articles
This is a guide to Powershell Send Mail. Here we discuss an introduction to Powershell Send Mail along with syntax and parameters, with examples. You can also go through our other related articles to learn more –