Skip to content

Instantly share code, notes, and snippets.

@Spartan-196
Last active March 14, 2022 19:02
Show Gist options
  • Save Spartan-196/b0973358cf4dbecc4dbf8f6f533bfc2c to your computer and use it in GitHub Desktop.
Save Spartan-196/b0973358cf4dbecc4dbf8f6f533bfc2c to your computer and use it in GitHub Desktop.
Anonymously Send to Internal SMTP

This can be used as part of a reporting mechnisizm on scheduled scripts to generate an email under defined conditions such as error results.

Utilises Send-MailMessage

#Setup Anon Credentials
$Username = "anonymous"
$Pass = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force 
$Creds = New-Object System.Management.Automation.PSCredential($Username, $Pass)

$emailBody = 
"
    Body Text
    
"

#Generate Email Hash
$emailHash = @{
    Subject    = "Email.Subject.line"
    Body       = $emailBody 
    SmtpServer = "your.smtp.server"
    #UseSSL     = $true #If STARTTLS is required
    #Port       = 587
    To         = "to@address"
    Priority   = "High"
    From       = "from@address" 
    Credential = $Creds
}

#Send mail with constructed hash
Send-MailMessage @emailHash

Alternate Options

$anonUsername = "anonymous"
$anonPassword = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force
$Creds = New-Object System.Management.Automation.PSCredential($anonUsername,$anonPassword)
Send-MailMessage -Subject "" -Body "Email from PS As $env:Username" -SmtpServer smtp.server.address -to "To@Address" -Priority High -From "User <From@Address>"  -Credential $Creds

From field formatting notes The From user show in outlook just as it does between the quotes.

From = "from@address" will show as from@address

From = "Last, First <from@address>" will show as Last, First <from@address>

From = '"Last, First" <from@address>' will show as Last, First <from@address>

From = '"Last, First" from@address' will show as Last, First <from@address>

From = "Last, First from@address" will show as Last, First <from@address>

From = "Last First from@address" will show as Last First <from@address>

From = "First Last from@address" will show as First Last <from@address>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment