Today I would like to share a function that I use a lot and which is quite simple, but very useful, it’s the “Send-MailMessage”.
In fact as the title suggests the goal is to send a mail in powershell, I use including this function in my script to warn one or a group of persons for the purpose of a script, by example to confirm the creation of a user sent to the admins group or to send logs in case of success or failure of a script.
How to send a mail in powershell?
Before you can send a mail it must have some information, the first is your SMTP server, it is a server that will forward your mail via SMTP (Simple Mail Transfer Protocol) Protocol. Then you must have an e-mail address that can send a message allowed by your e-mail server. And finally know your recipient (you guessed). Once all these collected information see how to use them.
The command we will use is “Send-MailMessage” to which we will add the following arguments:
-“From” corresponding with the mailing address of the mail
-“To” corresponding to the recipient of the mail
-“Subject” corresponding about the mail
-“Body” corresponding to the body of the mail
-“SmtpServer” corresponding to your server SMTP related
– “Attachments” has an attachment
Which can give the following command:
Send-MailMessage -From "From address" -To "Delivery address" -Subject "your subject" -SmtpServer "SMTP server" -Body "The body of the mail" -Attachements "the path to your file"
So now you know how to send a mail in powershell and I advise you to use it without moderation.