The results pdf was generated at the start and now an individual’s certificate has been generated. This post covers the email sending process.



Sample Data & Outcomes

No. Name Distance Time Expected Outcome*
1 Joe Blog DNS   Results email only
2 Julie Smith 8730 45:56 No email**
4 Fred Nurk 10145   Email Certificate PNG + Results
  • *Indicative only. Does not appear in the results
  • ** This outcome is for the first version of the the script: GenerateCertificates.ps1
    • The second version of the script generates a separately formatted certificate that includes time for results that include a time (only):GenerateCertificatesTIME

Setup for Emails

Needs Outlook 2016 installed and the following profile setup:

The Account Profile

As in both scripts:

$SendUsingAccount = "account@location.com.au"

Comment

This process requires the Classic version of Outlook, Outlook 2016 installed due to COM Objects used. Might be a way around this but this does work for now.

Checks and balances:

#
# Resolve send-as account once; fall back to default if not configured
#
$SendAccount = $Outlook.Session.Accounts.Item($SendUsingAccount)
if ($null -eq $SendAccount) {
    Write-Host "Account '$SendUsingAccount' not found - using default account" `
        -ForegroundColor Yellow
}

Script for Sending

    if (-not [string]::IsNullOrWhiteSpace($Email))
    {
        $Mail = $Outlook.CreateItem(0)

     $Mail.SendUsingAccount = $SendAccount

        $Mail.To = $Email

        $Mail.Subject =
            "Aberfeldie Masters Athletics - 1 Hour Track Run Certificate"

        $Mail.HTMLBody = @"
<p>Dear $Name,</p>

Add email body text here

</p>
        "@

        $Mail.Attachments.Add($CertificateFile) | Out-Null
        $Mail.Attachments.Add($ResultsPdf) | Out-Null

        # Only enable one of the next 2, not both.
        #$Mail.Send()
        $Mail.Save()
    }

For the DNS scenario, the first attachment is not made as there is no result/time and therefore no certificate in this case.

Testing and Sending the emails

For testing:

  • With Mail.Save() active (uncommented), Emails will be saved to the Drafts folder only.

Actually sending emails:

  • Enable Mail.Send() and comment out the other.

 TopicSubtopic
<  Prev:   Athletics Certificate Generator
   
 This Category Links 
Category:Application Dev Index:Application Dev