# Send an Email notification when Github Actions fails

*This blog was originally published on [RavSam blog](https://www.ravsam.in/blog/send-email-notification-when-github-action-fails/).*

We recently published a blog on [how to send a slack notification when a github action fails](https://www.ravsam.in/blog/send-slack-notification-when-github-actions-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:

```yml
name: Build

on:
  push:
    branches: main

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

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


![Github Action executed successfully](https://cdn.hashnode.com/res/hashnode/image/upload/v1631429432683/81_oYHDrx.png)*Github Action executed successfully*

### Add Send Email Action

[Dawid Dziurla](https://github.com/dawidd6/action-send-mail) 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:

```yml
- name: Send mail
  if: always()
  uses: dawidd6/action-send-mail@v2
  with:
    # 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](https://support.google.com/mail/answer/185833?hl=en-GB).

### Results

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 deliberately](https://cdn.hashnode.com/res/hashnode/image/upload/v1631429434733/oWKlWJVjo.png)*Github 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 Action](https://cdn.hashnode.com/res/hashnode/image/upload/v1631429436768/8_DnbUB4u.png)*Email 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](https://www.ravsam.in/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 [Twitter](https://twitter.com/ravgeetdhillon) • [LinkedIn](https://linkedin.com/in/ravgeetdhillon) • [Github](https://github.com/ravgeetdhillon) or send me an [Email](mailto:ravgeetdhillon@gmail.com).

— [Ravgeet](https://www.ravgeet.in), *Full Stack Developer and Technical Content Writer*

