Powershell script to automatically send HTML emails using gmail smtp

Hi please find the script/code below which can be used (as a example  use case) for sending automated reminders for filling time-sheets  to all employees on every Friday(can be any day depending on when you schedule it to run using windows task scheduler as per your requirements) using gmail gsuite or any other email providers:

Things Required:
  • SMTP outgoing port(587) allowed through firewalls
  • No-reply email which will be used for sending emails
  • Application password (as gmail blocks sending emails from scripts if direct credentials(username & normal password) are used and sends warning alerts and notifications to owner and eventually blocks the account if not rectified thus application password needs to be created and used to prevent that from happening,steps for creating same are given below)
  • Server which runs 24x7 where we will place and schedule our scripts to run at specific time.(You can use your local PC systems and execute script manually by double clicking if required)
Steps:
  • To generate Gmail App password go to https://myaccount.google.com/security and scroll down to section which says signing into to google and click on App password and create one.
    Note: This will be only available if you have 2 step verification on for gmail account you will be using so if you haven't done that already please make sure you do it first.
  • Create a new folder in D: drive on your system/server and name it "TimeSheets"
  • Inside "TimeSheets" create another folder and name it "Templates"
  • Open notepad and copy-paste the powershell script provided below in it and save it as TimeSheets.ps1 (Dont forget to select all files in file types while saving and changing extension to .ps1 extension as its important as it will save it as powershell script and not just a text file)
  • Open notepad and copy-paste the html code provided below in it and save it as TimeReminders.html (Do it in same manner we did for powershell script but with .html extension to save file as html type)
  • Create a Task in windows task scheduler (Start>>Search for Task scheduler>>Create Task and schedule it to run our TimeSheets.ps1 script whenever you want to send the reminder email to employees.
  • To schedule the PowerShell script, in action tab specify the following parameters:
    Action: Start a program
    Program\script: powershell or complete path to it C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe (without quotes since there's no space in there).
    Add arguments : -File (Specify path to powershell script here)
    Read more in details here
  • Thats it ! Please make sure to replace emails and YourGmailAppPassword in the script accordingly before use.
Powershell Script part:
#Author:Shivam Kumar
#Contact:tcbin.com
#Description:This script runs everyday on a provided schedule and automatically sends reminder emails to all employees for filling timesheets on link to portal.
$start = Get-Date -format "dd-MMM-yyyy HH:mm"
$EmailFrom = “no-reply@Yourcompanyname.com”
$EmailTo = “someperson@Yourcompanyname.com,some_teamdl@Yourcompanyname.com”
$Subject = “Reminder for filling timesheets”+" "+$start
$SMTPServer = “smtp.gmail.com”
$Reminder = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo)
$Reminder.Subject= $Subject
$Reminder.IsBodyHtml= $true
$Body= Get-Content ("D:\TimeSheets\Templates\TimeReminders.html")
$Reminder.Body = $Body + " "+ $start
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“no-reply@Yourcompanyname.com”, “YourGmailAppPasswordHere”);
$SMTPClient.Send($Reminder)
Templates part:
<html>
<body>
<head>
<style>
.button {
  background-color: #008CBA;;
  border: none;
  color: white!important;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
}
</style>
</head>
<h2>
Time Sheet Reminder</h2>
Please click the button below to visit Timesheet portal:<br />
<a class="button" href="http://LinkToYourTimeSheetPortals.com/">Timesheet</a>
Please ignore if already done.<br />
<b>Note: </b>Please do not reply to this email as its autogenerated &amp; not monitored contact your respective managers in case you are facing any issue.<br />
</body>
</html>

You might also want to read Things you might not know unless you Automate your first task using task scheduler

Comments

  1. Replies
    1. Hi for sending the mails with images...the simplest way is to store them on some server from where it is accessible to anyone over the internet and use the full url\address of that image in the html img tag....but i understand you might want to send the images from local machine...I have worked on that and will post a new article on it soon to help you and others to solve this problem...I will link the new post here as part 2 so please check back in a day or two thanks.

      Delete
  2. Did you write that post which described how to send html emails with embedded images (instead of images being on the server)?

    ReplyDelete

Post a Comment

Note:Please be gentle while commenting and avoid spamming as comment are anyways moderated thank you.

Popular posts from this blog

Enabling fixed Sidebar for blogger Contempo template for screen sizes smaller than 1440px

Adding Home Older Newer Post Buttons in new Blogger templates such as Contempo

Adding copy to clipboard button to every google code prettify pre blocks in blogger