How to Automate Welcome Emails for New Exchange On-Premises Users

    Send-WelcomeMail.ps1 | Powered by MAHARJAN-BINOD
    <#
    .SYNOPSIS
        Exchange On-Premise New User Welcome Mailer
    .DESCRIPTION
        Scans for new mailboxes created in the last 60 minutes and sends a 
        branded HTML welcome email with an attachment.
    .AUTHOR
        MAHARJAN-BINOD
    #>
    
    # --- 1. PRE-REQUISITES ---
    if (!(Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue)) {
        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    }
    
    # --- 2. CONFIGURATION ---
    $MinutesBack = 60 
    $TimeLimit = (Get-Date).AddMinutes(-$MinutesBack)
    $SMTPServer = "127.0.0.1" 
    $From = "HR-Onboarding@maharjan.com.np"
    $AdminCC = "exgadmin@maharjan.com.np"
    $ManagerBCC = "audit-logs@maharjan.com.np"
    $AttachmentPath = "C:\Reports\Combined_MailboxLoginReport.html"
    
    # --- 3. EXECUTION ---
    Write-Host "Scanning for new mailboxes since $TimeLimit..." -ForegroundColor Cyan
    
    $NewMailboxes = Get-Mailbox -ResultSize Unlimited | Where-Object { 
        $_.WhenMailboxCreated -gt $TimeLimit -and $_.CustomAttribute1 -ne "WelcomeSent" 
    }
    
    if ($NewMailboxes -ne $null) {
        foreach ($User in $NewMailboxes) {
            $OrgName = $User.Company
            if ([string]::IsNullOrWhiteSpace($OrgName)) { $OrgName = "MAHARJAN-BINOD" }
    
            $To = $User.PrimarySmtpAddress
            $DisplayName = $User.DisplayName
            $Subject = "Welcome to $OrgName - New Account Created"
    
            $Body = @"
            <!-- Beautiful HTML Email Template Logic Starts Here -->
            <div class='header'>Welcome to the Team, $DisplayName!</div>
            <!-- [Template Code Included in actual script] -->
    "@
    
            try {
                $Params = @{
                    SmtpServer = $SMTPServer
                    Port       = 25
                    From       = $From
                    To         = $To
                    Cc         = $AdminCC
                    Bcc        = $ManagerBCC
                    Subject    = $Subject
                    Body       = $Body
                    BodyAsHtml = $true
                    Priority   = "High"
                    ErrorAction = "Stop"
                }
    
                if (Test-Path $AttachmentPath) {
                    $Params.Add("Attachments", $AttachmentPath)
                }
    
                Send-MailMessage @Params
                Set-Mailbox -Identity $User.DistinguishedName -CustomAttribute1 "WelcomeSent"
                
                Write-Host "SUCCESS: Welcome mail sent to $To" -ForegroundColor Green
            }
            catch {
                Write-Warning "Failed to send to $To."
            }
        }
    }

    Leave a Reply

    Your email address will not be published. Required fields are marked *