Send an email alert when a customer changes state, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

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.

Runs Occurs whenever a customer is updated and Occurs whenever a customer is created. Configuration includes 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, and email recipient.

15-day free trial – unlimited tasks

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.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
shopify/customers/update
shopify/customers/create
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
email when a customer declines an invitation (boolean), email when a customer account is disabled (boolean), email when a customer initially sets an account password (boolean), email when a customer is invited (boolean), email recipient (email, required)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
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