Last active
August 22, 2023 03:15
-
-
Save plambrechtsen/138f0295958a421346497119bc9cc266 to your computer and use it in GitHub Desktop.
Send bulk email with outlook including send-as another account using a CSV input with PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
to | cc | firstname | fullname | |
---|---|---|---|---|
peter@email.local | shared@email.local; manager@email.local | Peter | Peter Lambrechtsen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$csvpath = ".\BulkEmail.csv" | |
$attachment = ".\Attachment.pdf" | |
$sender = 'shared@email.local' | |
Function Send-Email { | |
param ( | |
[String]$to, | |
[String]$cc, | |
[String]$firstname, | |
[String]$fullname | |
) | |
$body = @" | |
<html> | |
<body style="font-family:verdana"> | |
<h1>Mōrena $($fullname)</h1><br/> | |
</body> | |
<html> | |
"@ | |
$Outlook = New-Object -ComObject Outlook.Application | |
$mail = $Outlook.CreateItem(0) | |
$mail.subject = "IMPORTANT: Spam for $($firstname)" | |
$mail.SentOnBehalfOfName = $sender | |
$mail.SendUsingAccount = $Outlook.Session.Accounts | where {$_.DisplayName -eq $sender} | |
$mail.sender = $sender | |
$mail.To = $to | |
$mail.Cc = $cc | |
$mail.HTMLbody = $body | |
$attach = Get-ChildItem $attachment | |
$mail.Attachments.add($attach.FullName) | |
$mail.Send() | |
} | |
Import-Csv $csvpath | Foreach-Object { | |
Send-Email $_.to $_.cc $_.firstname $_.fullname | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment