Menu Close

PSSendgrid: Send email from PowerShell with Sendgrid

At the start of this year, I created a blog post on sending email from PowerShell through the SendGrid API.
I got a lot of comments on that post from people asking for more options, like sending attachments. So I did some more research and extended the PowerShell function from the previous post.
To make it more accessible, I have uploaded the cmdlet as a module to the PowerShell gallery. With this post, I present you PSSendgrid.

What is SendGrid?

SendGrid is a tool that is often used for marketing emails and newsletters. Along some other services it provides an API and SMTP service that you can use to make sending emails pretty straightforward.

You don’t authenticate against the account you use as the From-address. This means you don’t have to store those credentials. Unfortunately this also means that if you don’t prepare your environment, your email will be marked as unsafe, end up in the spam folder or be rejected (depending on the DMARC settings of the receiving party). And it should, because you are not authorized to use that address.
You do need to create an account with SendGrid, so you are not completely anonymous. But you should have control of the SPF records of the domain you are sending from and use Domain Authentication to make sure your email is not marked as spam.

Prerequisites

To make use of the module, you need to setup a SendGrid account so you can get a token. You can start using Send Grid for free. For example, you can create an account through Azure like this.

I want to mention again that your email will probably end up in the spam folder if you do not prepare the email security settings for your environment (like SPF, DKIM and DMARC). This risk gets bigger when using attachments.
To make sure your email is secure and not considered spam, see the following guides about changing your SPF record and setting up domain authentication.

https://sendgrid.com/docs/glossary/spf/
https://sendgrid.com/docs/ui/account-and-settings/how-to-set-up-domain-authentication/

For other pointers on starting with SendGrid and PowerShell, read my previous post.

PSSendgrid

I have uploaded PSSendgrid to the PowerShell gallery so you can easily access the function.
PSSendGrid has been tested on Windows with Windows PowerShell 5.1 and PowerShell 7. I expect it to also work on Linux and Mac, but have not tested the functionality.

To use it on your computer, first install the module with this cmdlet

Install-Module -Name PSSendGrid

Import the module by using

Import-Module -Name PSSendGrid

You now have the cmdlet Send-PSSendGridMail available to use. I will go through some examples of how to make use of the cmdlet.

Send a basic email

To send a basic email without attachments and HTML, use the following code:

(Notice how Outlook marks the email as not trusted. This is the default behavior)

Send an email with an attachment

You can add an attachment to an email in the following way


Send an email with an inline picture

You are also able to use an attachment as an inline picture for an HTML email. Not every email provider will work with this approach though, so to be sure to test your email.

Send an email to multiple people

You can use PowerShell logic to send mail in bulk. For example, to send the same email to multiple email addresses, you could use the following code:

Conclusion

I hope PSSendGrid will help you to send email with PowerShell and SendGrid. It is hard to test every kind of use case, so if you find something that does not work, don’t hesitate to create an issue here.

If you want to view the code, it is all available at my GitHub.

1 Comment

  1. Pingback:ICYMI: PowerShell Week of 31-July-2020 | PowerShell.org

Leave a Reply

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