Using Powershell from Outlook
You simply need to send a batch of e-mails with no BCC, CC, or TOs, where all recipients would not see each other addresses.
Your count of recipients depends on count of the addresses you provide to the script in one of the file used.
If you set it properly, you can send messages in the way, that will use correct name of the recipients, which is much better than general "Hello".
Aside of the e-mail address, you can provide values to replace, for example in order to inform clients about the Maintenance Window.
Prepare all your resources first, and then use the script for performing the tasks for you hassle free.
Please note, in some organizations, because of applied GPO (Group Policy Objects), it is not possible to run the Powershell script files (ps1), therefore it is required to start the Powershell application console, then copy & paste the script into console and run it.
It is standard CSV file with the extension of TXT, yet You can change the filename, extensions and defintelly the contents if Your have such demands for that. The number 0 or 1 at the end of each line represents the BOOL value. In my script it is used for the sending or not-sending any attachments. The file contents looks like this:
john.doe@yahoo.com,John,1
peter.newman@gmail.com,Peter,0
It is a standard TXT file (again in CSV like formatting) with values on each row of the content. You can change the values easily in every TXT file editor. The values that appears in the email sent are separated by underscores, because comma character might be used in the description or naming of some value. First value on the row is used for assigning the variables value. Therefore the file contents looks like this:
DateTime_15/02/2022 22:15
TimeZone_CET
EstTime_15 min.
EnvAffected_Production
Services_A297 - Device management, Ansible 181 - Data collection
The code loads previously mentioned text files, to grab the values about Maintenance Windows and also the emails of recipients, to process them and creates the HTML message for sending by Outlook, with configured e-mail address. Do you want to grab whole code and files mentioned for Your own setup? Feel free to grab them on my Git-Hub repository.
$sender = "info@domain.com";#needs to be configured in Outlook
function Invoke-SetProperty {
param(
[__ComObject] $Object,
[String] $Property,
$Value
)
[Void] $Object.GetType().InvokeMember($Property,"SetProperty",$NULL,$Object,$Value)
}
$Config = Get-Content -Path "C:\_DEV\config.txt"|ForEach-Object -Process { #adjust the file path and name
$Config = $_;
$ConfigArray = $Config.Split("_");
$Name=$ConfigArray['0'].Trim();
$Value=$ConfigArray['1'].Trim();
if($Name -eq "DateTime"){$DateTime = $Value};
if($Name -eq "TimeZone"){$TimeZone = $Value};
if($Name -eq "EstTime"){$EstTime = $Value};
if($Name -eq "EnvAffected"){$EnvAffected = $Value};
if($Name -eq "Services"){$Services = $Value};
$Subject = "Maintenance Window $DateTime "+ $TimeZone;
}
$htmltext = "<!DOCTYPE html><HTML><HEAD><STYLE>/* .ram{border:1px; background-color: rgb(255, 255, 255); border-color: #CCCCCC; border-style: solid; margin:auto; width: 600px; height:fit-content; padding: 40px; padding-top: 60px; padding-bottom: 60px; padding:'0';}*/ .nadpis1{font-family: Verdana; font-size: 22px; font-stretch: expanded; letter-spacing: 0px; word-spacing: 0px; color: #000000; margin:auto; text-align:left; font-weight: 400; text-decoration: none; font-style: normal; padding-bottom:30px;}.nadpis2{font-family: Verdana; font-size: 16px; letter-spacing: 0px; word-spacing: 1px; color: #000000; font-weight: 100; text-decoration: none; font-style: normal;}.nadpis3{font-family: Verdana; font-size: 12px; letter-spacing: 0px; word-spacing: 2px; color: #cccccc; font-weight: 100; text-decoration: none; font-style: normal;}.hodnota{color:rgb(0, 0, 0); font-family: Verdana; font-size: 12px; letter-spacing: 0px; word-spacing: 0px; font-weight: 150; text-decoration: none; font-style: normal;}.notification{font-family: Verdana; font-size: 12px; letter-spacing: 0px; word-spacing: 0px; color: #000000; font-weight: 100; text-decoration: none; font-style: normal;}table{border: 1px solid #CCCCCC; margin:auto; margin-top: 40px; width: 600px; height:fit-content; border-spacing: 20px; padding-left: 20px; padding-right: 20px; padding-bottom: 20px; padding-top: 40px; /* padding: 40px; padding-top: 40px; padding-bottom: 40px;*/}tr, th, td{height:fit-content; /*border: 1px solid #CCCCCC;*/}</style></HEAD><BODY> <CENTER> <TABLE class='table'> <tr><td><H1 class='nadpis1'>Scheduled Maintenance</H1><p class='nadpis2'>Upcoming scheduled maintenance notice</p><p class='notification'>This notice is to inform you that we will be performing scheduled maintenance that will affect the availability of <span style='color:rgb(0, 0, 0);font-weight:150px'>Axess & Axtract services</span>.</p></td></tr><tr><td><p class='notification'>Impact:<br/>During the execution of the scheduled activity, customer will experience the inability to establish a data session. No customer intervention is required.</p></td></tr><tr><td><h3 class='nadpis3'>Start time</h3><p class='hodnota'>DateTime</p></td></tr><tr><td><h3 class='nadpis3'>Estimated duration</h3><p class='hodnota'>EstTime</p></td></tr><tr><td><h3 class='nadpis3'>Environments affected</h3><p class='hodnota'>EnvAffected</p></td></tr><tr><td><h3 class='nadpis3'>Components affected</h3><p class='hodnota'>Services</p><br/></td></tr></TABLE> </CENTER></BODY></HTML>";
$result1 = $htmltext.replace('DateTime', $DateTime);
$result2 = $result1.replace('TimeZone', $TimeZone);
$result3 = $result2.replace('EstTime', $EstTime);
$result4 = $result3.replace('EnvAffected', $EnvAffected);
$result5 = $result4.replace('Services', $Services);
Get-Content -Path "C:\_DEV\addresses.txt"|ForEach-Object -Process {
$Adresa = $_;
$CharArray = $Adresa.Split(",");$mail=$CharArray['0'].Trim();
#$osloveni=$CharArray['1'].Trim();
$outlook = new-object -comobject outlook.application;
$email = $outlook.CreateItem(0);
$account = $email.Session.Accounts.Item($sender);
Invoke-SetProperty -Object $email -Property "SendUsingAccount" -Value $account;
$email.To = $mail;
$email.Subject = $Subject;
$attchmnt=$CharArray['2'].Trim();
if($attchmnt -eq "0"){
#-- do not add any file"
}else {
$email.Attachments.Add("C:\_DEV\addresses.txt");#adjust the filename and path with recipients addresses
};
$email.HTMLBody = $result5;
$email.Send();
}
In order to send properly the HTML message using Outlook, it is required to create a page with built-in CSS, so your HTML won't be plain text. In order to do so, be aware about some nasties of Outlook application. Create your content only in table content, as should you try to create the HTML body using DIV tags, it won't work properly. Moreover, even if you try to use CSS margin:auto style, Outlook will not place the content into the center of the message. In order to place the message into the center, there needs to be placed <CENTER></CENTER> tag around your content, to work properly. If you need to make the code of the Powershell script readable and understandable for you, I suggest to use some on-line minificator apps, where you can place the whole source code of the HTML page, and after the minification, only single line appears. This come handy, when you can place it directly into the Powershell code. So give it a try e.g. with this minifier.
Make sure, your resulting code does not contains double quotes - replace them with single quotes, as double quotes will be containing the HTML content. So should you have own HTML, make sure, that quotes within the HTML body will be only single quoted, because it can eventualy cause an issue in result.