Mechanic Get Mechanic
Task library

Send an email alert when a customer changes state

This task monitors for updates to a customer's state (account disabled, password set, invited, or invitation declined), and sends alert emails for any state changes you care about.

Try this task

When it runs

  • Occurs whenever a customer is updated
  • Occurs whenever a customer is created

What you choose

  • Email when a customer declines an invitation
  • Email when a customer account is disabled
  • Email when a customer initially sets an account password
  • Email when a customer is invited
  • Email recipient

Documentation

This task monitors for updates to a customer's state (account disabled, password set, invited, or invitation declined), and sends alert emails for any state changes you care about.

This task monitors for updates to a customer's state, and sends alert emails for any state changes you care about.

This task works by storing the customer's previous state, and comparing it to the new state. Therefore, this task cannot send emails for customers it hasn't seen before. This means that you may not see emails for some customers immediately after installing the task; emails will begin sending as customers are created and updated, allowing Mechanic to fill in its knowledge of customer state.

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/customers/update
shopify/customers/create
How task subscriptions work

Option types and flags

Task code

{% unless options.email_when_a_customer_declines_an_invitation__boolean or options.email_when_a_customer_account_is_disabled__boolean or options.email_when_a_customer_initially_sets_an_account_password__boolean or options.email_when_a_customer_is_invited__boolean %}
  {"error": "Choose at least one condition for sending an email."}
{% endunless %}

{% assign state_transition_descriptions = hash %}
{% assign state_transition_descriptions["DECLINED"] = "A customer has declined an invitation" %}
{% assign state_transition_descriptions["DISABLED"] = "A customer account has been disabled" %}
{% assign state_transition_descriptions["ENABLED"] = "A customer has set an initial account password" %}
{% assign state_transition_descriptions["INVITED"] = "A customer has been invited" %}

{% capture query %}
  query {
    customer(id: {{ customer.admin_graphql_api_id | json }}) {
      state
      previousStateMetafield: metafield(
        namespace: "mechanic"
        key: "previous-state"
      ) {
        id
        value
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% assign metafield = hash %}
  {% if options.email_when_a_customer_declines_an_invitation__boolean %}
    {% assign metafield["value"] = "DECLINED" %}
  {% elsif options.email_when_a_customer_account_is_disabled__boolean %}
    {% assign metafield["value"] = "DISABLED" %}
  {% elsif options.email_when_a_customer_initially_sets_an_account_password__boolean %}
    {% assign metafield["value"] = "ENABLED" %}
  {% elsif options.email_when_a_customer_is_invited__boolean %}
    {% assign metafield["value"] = "INVITED" %}
  {% endif %}
{% else %}
  {% assign metafield = result.data.customer.previousStateMetafield %}
{% endif %}

{% assign oldState = metafield.value %}
{% assign newState = result.data.customer.state %}

{% if oldState != newState %}
  {% if event.topic == "shopify/customers/update" and oldState != blank %}
    {% assign send_email = false %}

    {% if newState == "DECLINED" and options.email_when_a_customer_declines_an_invitation__boolean %}
      {% assign send_email = true %}
    {% elsif newState == "DISABLED" and options.email_when_a_customer_account_is_disabled__boolean %}
      {% assign send_email = true %}
    {% elsif newState == "ENABLED" and options.email_when_a_customer_initially_sets_an_account_password__boolean %}
      {% assign send_email = true %}
    {% elsif newState == "INVITED" and options.email_when_a_customer_is_invited__boolean %}
      {% assign send_email = true %}
    {% endif %}

    {% if send_email %}
      {% assign email_subject = "[" | append: shop.name | append: "] " | append: state_transition_descriptions[newState] %}

      {% capture email_body %}
        Hello,

        {{ state_transition_descriptions[newState] }}.

        Customer email: {{ customer.email | default: "(unknown)" }}
        Customer name: {{ customer.first_name | append: " " | append: customer.last_name | strip | default: "(unknown)" }}

        <a href="https://{{ shop.domain }}/admin/customers/{{ customer.id }}">Manage this customer</a>

        Thanks,
        Mechanic (for {{ shop.name }})
      {% endcapture %}

      {% action "email" %}
        {
          "to": {{ options.email_recipient__email_required | json }},
          "subject": {{ email_subject | json }},
          "body": {{ email_body | unindent | strip | newline_to_br | json }},
          "reply_to": {{ shop.customer_email | json }},
          "from_display_name": {{ shop.name | json }}
        }
      {% endaction %}
    {% endif %}
  {% endif %}

  {% action "shopify" %}
    mutation {
      customerUpdate(
        input: {
          id: {{ customer.admin_graphql_api_id | json }}
          metafields: [
            {
              id: {{ metafield.id | json }}
              namespace: "mechanic"
              key: "previous-state"
              type: "single_line_text_field"
              value: {% if event.preview %}"DISABLED"{% else %}{{ newState | json }}{% endif %}
            }
          ]
        }
      ) {
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endif %}
Mechanic Liquid reference

Default option values

Email when a customer declines an invitation
true
Email when a customer account is disabled
true
Email when a customer initially sets an account password
true
Email when a customer is invited
true