In this post, I would like to explain how to send an email with an HTML template in the PowerShell script using SMTP. We need to follow the below steps to send the emails. Already I posted regarding sending emails using PHP, and NodeJS. here we will know about how to send emails using PowerShell scripts.
Step 1: Create or prepare the HTML template which we need to send
Step 2: Create an SMTP account (In this post I am using Gmail SMTP)
Link for enable Gmail SMTP
Step 3: Create the PowerShell Script
Mention the Gmail from address, to address, SMTP Server, and Port
1 2 3 4 5 | $FromAddress="Your From [email protected]" $ToAddress="[email protected]" $emailSMTPServer = "smtp.gmail.com" $emailSMTPPort = 587 $emailUsername = "Your From [email protected]" |
Mention the SMTP password given here to have run the script in the background too without asking for a password
1 2 | $emailPassword = "Your Gmail SMTP Password" | ConvertTo-SecureString -AsPlainText -Force $emailCredentials = New-Object System.Management.Automation.PSCredential ($emailUsername, $emailPassword) |
Mention the subject for your email
1 | $Subject = "This is email from Powershell Script from Anil Labs" |
HTML Template
1 | $htmlTemplate = "<html><head></head><body><h1>Anil Labs</h1></body></html>"; |
To send the email
1 | Send-MailMessage -From $FromAddress -to $ToAddress -Subject $Subject -Body $htmlTemplate -BodyAsHtml -SmtpServer $emailSMTPServer -port $emailSMTPPort -UseSsl -Credential $emailCredentials |
Step 4: Run the script: Here we will get the error as
1 2 3 4 5 | PS C:\windows\system32> D:\ps-scripts\mailtest.ps1 File D:\ps-scripts\mailtest.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170. + CategoryInfo : SecurityError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnauthorizedAccess |
to overcome this error
Open the PowerShell and run as Administrator, and run the below command
1 | Set-ExecutionPolicy RemoteSigned |
and enter the ‘Y’
Step 5: Run the PowerShell script, now it will execute.
1 Comment
Sending HTML template emails from Oracle APEX - Anil Labs · September 13, 2023 at 4:16 pm
[…] the previous posts we have seen the how to send HTML template emails from PHP, NodeJS, PowerShell and we will see how to send HTML template emails from Oracle APEX, you can use the built-in email […]