-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSend-M365Digest-BasicAuth.ps1
More file actions
411 lines (346 loc) · 16.4 KB
/
Send-M365Digest-BasicAuth.ps1
File metadata and controls
411 lines (346 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#Requires -Version 5.1
<#
.SYNOPSIS
M365 Digest Email Campaign - Basic Authentication
.DESCRIPTION
Bulk email sending with Basic SMTP authentication featuring:
- Secure credential handling (multiple options)
- CSV data import with validation
- Template-based HTML emails
- Inline images and PDF attachments
- Batched sending with rate limiting
- Checkpoint-based resume capability
.PARAMETER ConfigMode
'Test' for reduced batch size, 'Production' for full campaign
.PARAMETER CredentialMethod
How to obtain SMTP credentials:
- 'Prompt' : Interactive prompt (default, most secure for manual runs)
- 'Environment' : From environment variables
- 'CredentialManager' : From Windows Credential Manager (requires CredentialManager module)
- 'SecureFile' : From encrypted XML file (user-specific DPAPI encryption)
.PARAMETER SmtpUsername
SMTP username (required for Environment method, optional override for others)
.NOTES
Requires: M365DigestEmailModule.psm1
SECURITY: This script does NOT store credentials in source code.
Choose a credential method appropriate for your use case:
- Interactive use: 'Prompt' (default)
- Scheduled tasks: 'SecureFile' or 'CredentialManager'
- CI/CD pipelines: 'Environment'
.AUTHOR
Jan Huebener
.VERSION
1.1.0
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[ValidateSet('Test', 'Production')]
[string]$ConfigMode = 'Production',
[Parameter(Mandatory = $false)]
[ValidateSet('Prompt', 'Environment', 'CredentialManager', 'SecureFile')]
[string]$CredentialMethod = 'Prompt',
[Parameter(Mandatory = $false)]
[string]$SmtpUsername,
[Parameter(Mandatory = $false)]
[string]$SecureFilePath = "$env:USERPROFILE\.m365digest\smtp_credential.xml",
[Parameter(Mandatory = $false)]
[string]$CredentialTarget = "M365-Digest-SMTP"
)
# ============================================================================
# CONFIGURATION
# ============================================================================
# Import module
$modulePath = Join-Path $PSScriptRoot "M365DigestEmailModule.psm1"
Import-Module $modulePath -Force -Verbose
# ============================================================================
# PATH CONFIGURATION - UPDATE THESE FOR YOUR ENVIRONMENT
# ============================================================================
$csvPath = "C:\Temp\recipients.csv" # Recipient CSV file
$htmlTemplate = Join-Path $PSScriptRoot "M365_Digest_Template.htm" # HTML template
$checkpointFile = "C:\Temp\smtp_send_checkpoint_m365digest.txt" # Checkpoint for resume
$failedRecipientsFile = "C:\Temp\failed_recipients.csv" # Failed emails log
# Inline Images (CID must match template references)
$inlineImages = @(
@{
ContentId = 'datagroup_logo'
FilePath = 'C:\temp\datagroup_logo.png'
},
@{
ContentId = 'm365_icon'
FilePath = Join-Path $PSScriptRoot 'm365_icon.png'
},
@{
ContentId = 'exchange_icon'
FilePath = Join-Path $PSScriptRoot 'exchange_icon.png'
},
@{
ContentId = 'sharepoint_icon'
FilePath = Join-Path $PSScriptRoot 'sharepoint_icon.png'
}
)
# Attachments (optional - comment out if not needed)
$attachments = @(
# "C:\temp\Document1.pdf",
# "C:\temp\Document2.pdf"
)
# ============================================================================
# SMTP CONFIGURATION - UPDATE THESE FOR YOUR ENVIRONMENT
# ============================================================================
$smtpConfig = @{
Server = "smtp.office365.com"
Port = 587
EnableSsl = $true
From = "noreply@yourdomain.com" # UPDATE: Your sender address
Bcc = "admin@yourdomain.com" # UPDATE: BCC for monitoring (optional)
Subject = "Microsoft 365 Monthly Digest - What's new?"
}
# Batch Configuration
$batchConfig = @{
BatchSize = 20
WindowMinutes = 3.0
MaxRetries = 3
}
# Test Mode - reduced settings for testing
if ($ConfigMode -eq 'Test') {
Write-Host "`n[TEST MODE] Using reduced batch settings" -ForegroundColor Yellow
$batchConfig.BatchSize = 2
$batchConfig.WindowMinutes = 0.1
}
# ============================================================================
# SECURE CREDENTIAL FUNCTIONS
# ============================================================================
function Get-SmtpCredentialSecure {
<#
.SYNOPSIS
Retrieves SMTP credentials using the specified method
#>
param(
[string]$Method,
[string]$Username,
[string]$SecureFilePath,
[string]$CredentialTarget
)
switch ($Method) {
'Prompt' {
Write-Host " [Prompt] Enter SMTP credentials..." -ForegroundColor Cyan
$cred = Get-Credential -Message "Enter SMTP credentials for M365 Digest"
if (-not $cred) {
throw "Credential prompt was cancelled"
}
return $cred
}
'Environment' {
Write-Host " [Environment] Loading from environment variables..." -ForegroundColor Cyan
$envUser = $env:M365_SMTP_USERNAME
$envPass = $env:M365_SMTP_PASSWORD
if ([string]::IsNullOrWhiteSpace($envUser)) {
throw "Environment variable M365_SMTP_USERNAME is not set"
}
if ([string]::IsNullOrWhiteSpace($envPass)) {
throw "Environment variable M365_SMTP_PASSWORD is not set"
}
$securePass = ConvertTo-SecureString $envPass -AsPlainText -Force
return New-Object System.Management.Automation.PSCredential($envUser, $securePass)
}
'CredentialManager' {
Write-Host " [CredentialManager] Loading from Windows Credential Manager..." -ForegroundColor Cyan
# Check if CredentialManager module is available
if (-not (Get-Module -ListAvailable -Name CredentialManager)) {
throw "CredentialManager module not installed. Install with: Install-Module CredentialManager"
}
Import-Module CredentialManager -ErrorAction Stop
$cred = Get-StoredCredential -Target $CredentialTarget
if (-not $cred) {
throw "Credential '$CredentialTarget' not found in Windows Credential Manager. Create it with: New-StoredCredential -Target '$CredentialTarget' -UserName 'user@domain.com' -Password 'password' -Persist LocalMachine"
}
return $cred
}
'SecureFile' {
Write-Host " [SecureFile] Loading from encrypted file..." -ForegroundColor Cyan
if (-not (Test-Path $SecureFilePath)) {
Write-Host "`n Secure credential file not found. Creating one now..." -ForegroundColor Yellow
Write-Host " File: $SecureFilePath" -ForegroundColor Gray
# Ensure directory exists
$secureDir = Split-Path $SecureFilePath -Parent
if (-not (Test-Path $secureDir)) {
New-Item -Path $secureDir -ItemType Directory -Force | Out-Null
}
# Prompt for credentials and save encrypted
$newCred = Get-Credential -Message "Enter SMTP credentials to save (encrypted with your Windows account)"
if (-not $newCred) {
throw "Credential prompt was cancelled"
}
$newCred | Export-Clixml -Path $SecureFilePath
Write-Host " [OK] Credentials saved to: $SecureFilePath" -ForegroundColor Green
Write-Host " Note: This file can only be decrypted by your Windows account on this machine.`n" -ForegroundColor Gray
return $newCred
}
# Load existing encrypted credential
$cred = Import-Clixml -Path $SecureFilePath
return $cred
}
default {
throw "Unknown credential method: $Method"
}
}
}
# ============================================================================
# MAIN EXECUTION
# ============================================================================
try {
Write-Host "`n" -NoNewline
Write-Host "========================================================" -ForegroundColor Cyan
Write-Host " M365 Monthly Digest Email Campaign (Basic Auth) " -ForegroundColor Cyan
Write-Host "========================================================" -ForegroundColor Cyan
Write-Host " Mode: $ConfigMode" -ForegroundColor $(if ($ConfigMode -eq 'Test') { 'Yellow' } else { 'Green' })
Write-Host " Credential Method: $CredentialMethod" -ForegroundColor Gray
Write-Host "========================================================`n" -ForegroundColor Cyan
# ========================================================================
# STEP 1: Validate file paths
# ========================================================================
Write-Host "[1/5] Validating file paths..." -ForegroundColor Cyan
if (-not (Test-Path $csvPath)) {
throw "CSV file not found: $csvPath"
}
Write-Host " [OK] CSV file: $csvPath" -ForegroundColor Green
if (-not (Test-Path $htmlTemplate)) {
throw "HTML template not found: $htmlTemplate"
}
Write-Host " [OK] HTML template: $htmlTemplate" -ForegroundColor Green
# ========================================================================
# STEP 2: Validate inline images
# ========================================================================
Write-Host "`n[2/5] Validating inline images..." -ForegroundColor Cyan
$missingImages = 0
foreach ($img in $inlineImages) {
if (Test-Path $img.FilePath) {
$size = [math]::Round((Get-Item $img.FilePath).Length / 1KB, 1)
Write-Host " [OK] $($img.ContentId): ${size}KB" -ForegroundColor Green
}
else {
Write-Host " [WARN] Missing: $($img.FilePath)" -ForegroundColor Yellow
$missingImages++
}
}
if ($missingImages -gt 0) {
Write-Host " [!] $missingImages image(s) missing - emails may display incorrectly" -ForegroundColor Yellow
}
# ========================================================================
# STEP 3: Get credentials securely
# ========================================================================
Write-Host "`n[3/5] Obtaining SMTP credentials..." -ForegroundColor Cyan
$credential = Get-SmtpCredentialSecure `
-Method $CredentialMethod `
-Username $SmtpUsername `
-SecureFilePath $SecureFilePath `
-CredentialTarget $CredentialTarget
# Update SMTP config with credential
$smtpConfig.Credential = Get-EmailAuthenticationCredential `
-AuthMethod 'Basic' `
-Username $credential.UserName `
-Password $credential.GetNetworkCredential().Password
# Update From address to match credential username if not explicitly set
if ($smtpConfig.From -match 'yourdomain.com') {
$smtpConfig.From = $credential.UserName
Write-Host " [INFO] Using credential username as From address" -ForegroundColor Gray
}
Write-Host " [OK] Credentials loaded for: $($credential.UserName)" -ForegroundColor Green
# ========================================================================
# STEP 4: Load and validate recipients
# ========================================================================
Write-Host "`n[4/5] Loading recipient data..." -ForegroundColor Cyan
$csvData = Import-Csv -LiteralPath $csvPath -Delimiter ';' -Encoding UTF8
# Build recipient objects with validation
$recipients = @()
$invalidEmails = @()
$emailRegex = '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
foreach ($row in $csvData) {
$email = ($row.email).Trim()
# Skip empty emails
if ([string]::IsNullOrWhiteSpace($email)) { continue }
# Validate email format
if ($email -notmatch $emailRegex) {
$invalidEmails += $email
Write-Host " [WARN] Invalid email skipped: $email" -ForegroundColor Yellow
continue
}
# Build replacement hashtable for this recipient
$replacements = @{
'CARD1_TITLE' = "New Teams Features"
'CARD1_CONTENT' = "Microsoft Teams introduces new collaboration features including enhanced meeting recordings and AI-powered meeting summaries."
'CARD1_LINK' = "https://admin.microsoft.com/?ref=MessageCenter/:/messages/MC1069560"
'CARD2_TITLE' = "Exchange Online Updates"
'CARD2_CONTENT' = "Enhanced security features now available for Exchange Online mailboxes, including improved phishing protection."
'CARD2_LINK' = "https://admin.microsoft.com/?ref=MessageCenter/:/messages/MC1134178"
'CARD3_TITLE' = "SharePoint Improvements"
'CARD3_CONTENT' = "New document management capabilities in SharePoint Online with AI-powered search and classification."
'CARD3_LINK' = "https://admin.microsoft.com/?ref=MessageCenter/:/messages/MC1069560"
'UNSUBSCRIBE_LINK' = "https://www.yourdomain.com/unsubscribe?email=$email"
}
# Personalize if DisplayName is available
if ($row.PSObject.Properties.Name -contains 'DisplayName_email' -and $row.DisplayName_email) {
$replacements['CARD1_CONTENT'] = "Hello $($row.DisplayName_email), " + $replacements['CARD1_CONTENT']
}
$recipients += [PSCustomObject]@{
Email = $email
Replacements = $replacements
}
}
Write-Host " [OK] Valid recipients: $($recipients.Count)" -ForegroundColor Green
if ($invalidEmails.Count -gt 0) {
Write-Host " [WARN] Invalid emails skipped: $($invalidEmails.Count)" -ForegroundColor Yellow
}
if ($recipients.Count -eq 0) {
throw "No valid recipients found in CSV file"
}
# ========================================================================
# STEP 5: Display summary and confirm
# ========================================================================
Write-Host "`n[5/5] Campaign Summary" -ForegroundColor Cyan
Write-Host "========================================================" -ForegroundColor Cyan
Write-Host " Recipients: $($recipients.Count)"
Write-Host " Batch Size: $($batchConfig.BatchSize)"
Write-Host " Window Interval: $($batchConfig.WindowMinutes) minutes"
Write-Host " Max Retries: $($batchConfig.MaxRetries)"
Write-Host " Sender: $($smtpConfig.From)"
Write-Host " Subject: $($smtpConfig.Subject)"
Write-Host " Checkpoint: $checkpointFile"
Write-Host "========================================================`n" -ForegroundColor Cyan
if ($ConfigMode -eq 'Production' -and $recipients.Count -gt 10) {
Write-Host "[!] PRODUCTION MODE - Sending to $($recipients.Count) recipients" -ForegroundColor Yellow
Write-Host " Press Ctrl+C within 5 seconds to abort..." -ForegroundColor Yellow
Start-Sleep -Seconds 5
}
# Template Configuration
$templateConfig = @{
TemplatePath = $htmlTemplate
Encoding = 'UTF8'
InlineImages = $inlineImages
Attachments = $attachments
}
# Send bulk emails
$bulkParams = @{
Recipients = $recipients
TemplateConfig = $templateConfig
SmtpConfig = $smtpConfig
BatchSize = $batchConfig.BatchSize
WindowMinutes = $batchConfig.WindowMinutes
MaxRetries = $batchConfig.MaxRetries
CheckpointPath = $checkpointFile
}
Send-BulkHtmlEmail @bulkParams
Write-Host "`n========================================================" -ForegroundColor Green
Write-Host " CAMPAIGN COMPLETED SUCCESSFULLY" -ForegroundColor Green
Write-Host "========================================================`n" -ForegroundColor Green
}
catch {
Write-Host "`n========================================================" -ForegroundColor Red
Write-Host " CAMPAIGN FAILED" -ForegroundColor Red
Write-Host "========================================================" -ForegroundColor Red
Write-Host " Error: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "`n Stack Trace:" -ForegroundColor Gray
Write-Host $_.ScriptStackTrace -ForegroundColor Gray
Write-Host "========================================================`n" -ForegroundColor Red
exit 1
}