Send Email – PS Script

send email using powershell

We can use powershell script to send email to anyone or any Distribution list which will contains group of users.

you can also use this script with task scheduler to schedule it to run or use with other mean of automation to trigger email delivery.

NOTe:

this needs to be run from server which is domain joined and fill in the required field specially your smtp server name.

Write-Host "Welcome to the demo of sending email in PowerShell"
$from="email@domain.com"

Write-Host "From address will be" $from

$to="email@domain.com"
$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 = "mailhost.email@domain.com"

$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"

 

Exit mobile version