-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendTestEmail.ps1
49 lines (38 loc) · 1.85 KB
/
sendTestEmail.ps1
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
#See https://mailchimp.com/developer/marketing/guides/quick-start/
param([hashtable] $params)
$server = $params['Username'];
$apiKey = $params['Password'];
$messageName = $params['MessageName'];
$testRecipients = $params['TestRecipients'];
$debugFile = $params['DebugFile'];
$scriptRoot = $params['ScriptRoot'];
$templatesFolderName = "Templates"
if ([string]::IsNullOrEmpty($scriptRoot)) {
$scriptRoot = $PSScriptRoot
}
. "$scriptRoot\common.ps1"
Write-Debug $debugFile "Called sendTestEmail script with parameters" $params
Write-Debug $debugFile "Got test recipients to send to" $testRecipients
#Get the folder id for the "Templates" folder, which is where to the predefined templates should have been created
$templatesFolderId = Get-CampaignFolderIdForName $templatesFolderName
Write-Debug $debugFile "Found campaign folder with id ${templatesFolderId} for name ${templatesFolderName}"
#Get the id of the template campaign to send as a test
$campaignId = Get-CampaignIdForNameInFolder $templatesFolderId $messageName
Write-Debug $debugFile "Found campaign with id ${campaignId} for name ${messageName} in folder with id ${templatesFolderId}"
if ([string]::IsNullOrEmpty($campaignId)) {
return null
}
#Create a list of recipient email addresses
$testRecipientsObj = ConvertFrom-Json $testRecipients
$testEmails = [System.Collections.ArrayList]@()
Foreach($testRecipient in $testRecipientsObj) {
$testEmails.Add($testRecipient.Email)
}
#Trigger a test send of the campaign
$body = [hashtable]@{
"send_type" = "html"
"test_emails" = $testEmails
}
$bodyJson = $body | ConvertTo-Json
Write-Debug $debugFile "Sending email test details for campaign ${campaignId}" $bodyJson
$results = Invoke-RestMethod -UseBasicParsing -Method 'Post' -Uri "https://${server}.api.mailchimp.com/3.0/campaigns/${campaignId}/actions/test" -Headers @{ "Authorization" = "Bearer: $apiKey" } -Body $bodyJson