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)

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

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