-
-
Save pkskelly/0b16f9892234e88c50c3 to your computer and use it in GitHub Desktop.
$Username ="azure_*********@azure.com" | |
$Password = ConvertTo-SecureString "********" -AsPlainText -Force | |
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password | |
$SMTPServer = "smtp.sendgrid.net" | |
$EmailFrom = "[email protected]" | |
$EmailTo = "[email protected]" | |
$Subject = "SendGrid test" | |
$Body = "SendGrid testing successful" | |
Send-MailMessage -smtpServer $SMTPServer -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body $Body |
How to add the attachment file while sending the mail in SendGrid using powershell
You can add a parameter "-Attachments" to the Send-MailMessage cmdlet and specify a comma separated list of file names.
Sending passwords over the script is not secure, instead, use API Keys provided by SendGrid to send emails with or without attachments
Using API Keys with PowerShell, for sending attachments is the tricky part. SendGrid documentation too does have much on this. Can any one help here.
Using API Keys with PowerShell, for sending attachments is the tricky part. SendGrid documentation too does have much on this. Can any one help here.
You can store the API Keys in KeyVault and retrieve the same from it.
$SENDGRID_API_KEY = (Get-AzKeyVaultSecret -VaultName $VaultName -Name $ApiKeyName).SecretValueText
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer " + $SENDGRID_API_KEY)
$headers.Add("Content-Type", "application/json")
create a body json which has mail body content which has from and destination mail address details, subject and bodyContent of Mail
finally, call the below method.
$response = Invoke-RestMethod -Uri https://api.sendgrid.com/v3/mail/send -Method Post -Headers $headers -Body $bodyJson
The operation has timed out.
Thanks. Nice Script. Works perfect. Only one thing 1 had changed.
$Username ="azure_*********@azure.com"
should be
$Username ="apikey"
Username is always apikey
The password defines the configuration in Sendgrid
How to add the attachment file while sending the mail in SendGrid using powershell