Skip to content

Instantly share code, notes, and snippets.

@TomasBouda
Created April 28, 2019 19:42
Show Gist options
  • Save TomasBouda/7238ae40faf49fda16e6ed9caff3b6db to your computer and use it in GitHub Desktop.
Save TomasBouda/7238ae40faf49fda16e6ed9caff3b6db to your computer and use it in GitHub Desktop.
Send email trough SendGrid SMTP relay with powershell
function Send-GridEmail(){
Param(
[Parameter(Mandatory=$true)]
[string]$From,
[Parameter(Mandatory=$true)]
[string[]]$To,
[Parameter(Mandatory=$true)]
[string]$Subject,
[Parameter(Mandatory=$true)]
[string]$Body,
[Parameter(Mandatory=$true)]
[string[]]$Cc,
[Parameter(Mandatory=$false)]
[string[]]$Attachments = $null
)
$Username ="apikey"
$Password = ConvertTo-SecureString "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" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password
$SMTPServer = "smtp.sendgrid.net"
Send-MailMessage -SmtpServer $SMTPServer -Credential $credential -UseSsl -Port 587 -From $From -To $To -Cc $Cc -Subject $Subject -Body $Body -BodyAsHtml -Attachments $Attachments -DeliveryNotificationOption OnSuccess, OnFailure
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment