mailslurp-examples - powershell-email-send-ps1

https://github.com/mailslurp/examples

Table of Contents

powershell-email-send-ps1/send.ps1

# set API_KEY env variable to MailSlurp API Key
$apiKey = $Env:API_KEY
Write-Output "Running send script with key $apiKey"

# get inbox to send with
$inboxes = Invoke-WebRequest -Uri "https://api.mailslurp.com/inboxes" -Headers @{"x-api-key"=$apiKey;} | ConvertFrom-Json
$email = $inboxes[0].emailAddress
$inboxId = $inboxes[0].id

# Send the email
$params = @{
 "to"=@($email);
 "subject"="Testing 123";
 "body"="Hello";
}
$status = Invoke-WebRequest `
	-Uri "https://api.mailslurp.com/inboxes/$inboxId" `
	-Method POST `
	-Body ($params|ConvertTo-Json) `
	-ContentType "application/json" `
	-Headers @{"x-api-key"=$apiKey;} | Select-Object -Expand StatusCode

Write-Output "Email sent with status $status"

powershell-email-send-ps1/access.ps1

$API_KEY = $env:API_KEY
$response = Invoke-RestMethod -Uri "https://api.mailslurp.com/inboxes?inboxType=SMTP_INBOX" -Method Post -Headers @{"x-api-key" = $API_KEY}
$inboxId = $response.id
#<gen>ps1_access_mailbox
# download access details for an inbox as .env file
Set-Location $PSScriptRoot
Invoke-WebRequest -OutFile ".env" -Uri "https://api.mailslurp.com/inboxes/imap-smtp-access/env?inboxId=$inboxId" -Headers @{"x-api-key" = $API_KEY}

# source the .env and connect using variables
Get-Content ".env" | ForEach-Object {
    $keyValue = $_.Split('=', 2)
    if ($keyValue.Count -eq 2)
    {
        $envName = $keyValue[0].Trim()
        $envValue = $keyValue[1].Trim()
        # Remove leading and trailing double quotes from the value
        if ($envValue.StartsWith('"') -and $envValue.EndsWith('"'))
        {
            $envValue = $envValue.Substring(1, $envValue.Length - 2)
        }
        [Environment]::SetEnvironmentVariable($envName, $envValue, [System.EnvironmentVariableTarget]::Process)
    }
}
#</gen>
if (-not (Test-Path env:IMAP_SERVER_HOST)) {
    throw "Environment variable IMAP_SERVER_HOST does not exist"
}

powershell-email-send-ps1/Makefile

-include ../.env

test:
	pwsh send.ps1
	pwsh access.ps1