How To Send an Email With an Attachment Using PowerShell and SendGrid API

How To Send an Email With an Attachment Using PowerShell and SendGrid API

In this article let us discuss Sending email with attachment using Powershell and Send Grid. This includes the below steps (Also Available Github)

In this article let us discuss Sending email with attachment using Powershell and Send Grid. This includes the below steps (Also Available Github)

Advantages:

  • This can be used to send emails in Azure DevOps CI/CD Pipeline.
  • One of the important area is once your test is executed if you want to Send Test Report to respective stakeholders this will be very useful.
  • This will also answer the question of_ “How to send Email in Azure DevOps CI/CD Pipeline”_ 

Pre-Requisite: You need to have SendGrid Account (If you don’t have one create a Free Account)

Step 1: Register Sender in Send Grid

Step 2: Generate Send Grid API Key

Step 3: Create a Powershell function to send an email with attachment using SendGrid API

Step 4: Call the function using Powershell

Register Sender in SendGrid

Login to SendGrid account with your username and password 

(_Note: If you do not have a SendGrid account create one using SendGrid [SignUp_](https://signup.sendgrid.com/))

Navigate to Settings > Sender Authentication

Click on Verify Single Sender

In a Create Sender Form, Enter all the mandatory details and Click on Create

Why do we need to add the Sender?
  1. Adding and verification of the sender is a must in SendGrid for security reasons.
  2. Without adding the sender, you will not be able to send the email to any person.
  3. The added sender email is used as From address in SendGrid API post request.
  4. Without adding the sender and verifying the sender, SendGrid will not allow you to send any emails.
  5. Important! Once you add the Sender, You will get an email to verify the Sender, you need to click on the Verify Sender link to complete the process of adding the sender.

Generating API Key in SendGrid

API Key is very important and mandatory if you are using SendGrid API to Send Emails

In order to Generate API Key, Go to Settings > Click on API Keys

Now, Click on Create API Key

Enter Your API Key Name, Choose Access and Click on Create and View

Once everything is done, You will see API Key

Important!: Copy the API Key and save Somewhere, If you lose this API key you can’t get it back.

Now we have successfully added the sender and generated the API key.

Next, We need to create a Powershell script to Call the API and Send an Email.

api jenkins powershell azure devops powershell script devops

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Create Azure Infrastructure with Azure DevOps and Azure CLI Powershell scripts

In Azure DevOps, Pipelines can be used to create Azure infrastructure using Azure CLI and Powershell. This blog shows how to create a simple resource group in Azure using Azure DevOps Pipelines.

Create, Build, Deploy and Configure an Azure Function with Azure DevOps and Azure CLI

How to create, build, deploy and configure an Azure Function using Azure DevOps, Azure CLI and Powershell.

Create, Build, Deploy and Configure an Azure App Service with Azure DevOps and Azure CLI

This post shows how to create, build, deploy and configure an Azure App Service using Azure DevOps, Azure CLI and Powershell.

Jenkins Is Getting Old — It’s Time to Move On

After using Jenkins on several projects, we say it's time to move on. Jenkins is left behind with his old approach — found out more!

Top 10 API Security Threats Every API Team Should Know

Learn what are the most important API security threats engineering leaders should be aware of and steps you can take to prevent them