Skip to content

Latest commit

 

History

History
84 lines (59 loc) · 2.22 KB

README.md

File metadata and controls

84 lines (59 loc) · 2.22 KB

PsTokens

PowerShell token-replacement library.

Usage

Tokens follow the pattern __TokenName__ (case insensitive).

Assuming that the content of .\template.txt is:

FN: __FirstName__
LN: __LastName__

This PowerShell script:

PS> $content = Get-Content .\template.txt | Merge-Tokens -tokens @{'FirstName' = 'foo'; 'LastName' = 'bar'}

Will populate the $content variable with this text:

FN: Foo
LN: Bar

Email Merge

Send email messages to a list, pausing as desired as to not get caught by server rules.

# gmail settings
$smtpServer = 'smtp.gmail.com'
$port = 587
$credential = Get-Credential

$from = 'First Last <[email protected]>'
$recipients = @(
    @{firstName='Foo';lastName='Bar';address='[email protected]'},
    @{firstName='Bar';lastName='Foo';address='[email protected]'},
    @{firstName='Jane';lastName='Doe';address='[email protected]'},
    @{firstName='John';lastName='Doe';address='[email protected]'}
    )
$subject = 'lorem ipsum'

# tokens are cAsE SenSitivE and must match the values in the recipient list
$template = @"
Dear __firstName__ __lastName__,

Veniam ratione velit tenetur sunt eligendi. Veniam commodi et ut voluptates sint nulla eos. Sint ut sunt nisi occaecati tempore non. Minima cupiditate quis quia incidunt dolore distinctio qui.

First Last
"@

$counter=0
$wait=5 # time to wait between batches of messages
$batchSize = 2 # number of messages to send in each batch

$recipients | % {

    $counter+=1

    Write-Verbose "Sending message to $($_.firstName) $($_.lastName) <$($_.address)>"

    # replace values in template w/ hash's values
    $body = $template | Merge-Tokens -tokens $_
    Write-Verbose "----------`n$body"

    Send-MailMessage -SmtpServer $smtpserver -Port $port -UseSsl -Credential $credential -From $from -To $_.address -Subject $subject -Body $body 

    if ($counter % $batchSize -eq 0) {
        Write-Verbose "Sleeping for $wait seconds..."
        Start-Sleep -s $wait
    }

}

Write-Verbose "Sent $counter messages"

Contributions