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.
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.
For other pointers on starting with SendGrid and PowerShell, read my previous post.
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
Update 29-07-2021: Multiple receivers, in To, CC or BCC, are now supported within the cmdlet!
You can use the following syntax:
You can also still 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:
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.