Ravgeet Dhillon
Ravgeet's Blog


Ravgeet's Blog

Send an Email notification when Github Actions fails

Send an Email notification when Github Actions fails

Send a custom email notification to multiple recipients when a Github Actions workflow fails or succeeds.

Ravgeet Dhillon's photo
Ravgeet Dhillon
·Dec 4, 2020·
Play this article

This blog was originally published on RavSam blog.

We recently published a blog on how to send a slack notification when a github action fails. We got a great response from the open-source community. Some of the community members asked us about how they can send an email notification when a Github Action fails. So to take in the request, today we will see how we can build a workflow that allows us to achieve this purpose.

Github has this feature natively that sends an email when a Github Action fails. It works efficiently when you are working on an individual project. However, when working in a team, we often want to notify more than one team member about the possible failure of the workflow.

Create a sample workflow

Let us write a simple workflow that prints the infamous Hello World. Create a new file build.yml in .github/workflows directory and add:

name: Build

    branches: main

    runs-on: ubuntu-latest
      - uses: actions/checkout@v2

      - name: Hello World
        run: echo Hello, world!

Github Action executed successfullyGithub Action executed successfully

Add Send Email Action

Dawid Dziurla has published a Github action that allows us to configure a lot of aspects related to sending the emails. We just need to add the below step to our workflow:

- name: Send mail
  if: always()
  uses: dawidd6/action-send-mail@v2
    # mail server settings
    server_address: smtp.gmail.com
    server_port: 465
    # user credentials
    username: ${{ secrets.EMAIL_USERNAME }}
    password: ${{ secrets.EMAIL_PASSWORD }}
    # email subject
    subject: ${{ github.job }} job of ${{ github.repository }} has ${{ job.status }}
    # email body as text
    body: ${{ github.job }} job in worflow ${{ github.workflow }} of ${{ github.repository }} has ${{ job.status }}
    # comma-separated string, send email to
    to: johndoe@gmail.com,doejohn@gmail.com
    # from email name
    from: John Doe

Use echo "${{ toJson(github) }}" to get more workflow context variables.

The if: always() directive tells the Github Actions to always run this step regardless of whether the preceding steps have been executed successfully or not. We use the workflow’s context variables to build our email subject and body. Don’t forget to add your username and password as Action secrets.

Make sure to use the App-Specific password for the above action. Learn how to create an app-specific password for GMail.


Before testing the action in use, let us deliberately fail the action. All we need to do is update the Hello World step’s run command to echo Hello, world! && exit 1. exit 1 sets an exit status of 1 which tells the Github Actions that some kind of error has occurred. Let push our code and see what happens.

Github Actions workflow failed deliberatelyGithub Actions workflow failed deliberately

From the above screenshot, we can see that the Send mail step was executed even though the previous step failed. Let us check our inbox for the email about the failure.

Email Notification sent about failed Github ActionEmail Notification sent about failed Github Action

Sweet! We can see that an email notification was sent to our recipients. The subject and body were populated with the appropriate repository and workflow.

Github Actions is a great CI/CD tool. By using the right actions, we can build workflows that help in boosting team productivity at any workspace.

Thanks for reading 💜

If you enjoyed my blog, follow me for more informative content like this.

I publish a monthly newsletter in which I share personal stories, things that I am working on, what is happening in the world of tech, and some interesting dev related posts which I across while surfing on the web.

Connect with me through TwitterLinkedInGithub or send me an Email.

Ravgeet, Full Stack Developer and Technical Content Writer

Did you find this article valuable?

Support Ravgeet Dhillon by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this