Email customers when tagged with Mechanic.

Mechanic is the one-tool-does-it-all automation app for Shopify. :)

Email customers when tagged

by Isaac Bowen (team@usemechanic.com)

Automatically send out an email to your customers when a tag is added to their account. Useful for sending approval emails, welcome emails if the customer was created with a certain tag, or any other emails related to an update to the customer's status. The most common use of this task is to send out emails to customers when their accounts are approved, but you can use this task to email customers any kind of alert you like, triggered by a simple tag. For example, some themes include an email subscription form that creates a customer record, pre-tagged with "email subscribe", or something to that effect.

Runs when a customer is created, when a customer is updated, and when a user triggers the task. Configuration includes tag to watch for, ignore tag case, autotag customers after emailing, email subject, and email body.

15-day free trial – unlimited tasks

Documentation

Important note: After saving this task for the first time, click the "Run task" button. Mechanic will then scan your customers to find those who already have the desired tag, and will make a note of them, so as to not email them in the future. (We require this to avoid suddenly sending a flood of emails to customers that were tagged before the Mechanic task was created.)

Enable the "Autotag customers after emailing" option to have Mechanic add "YOURTAGHERE-email-sent" to the customer's tags, when their email is sent out.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Events
when a customer is created (shopify/customers/create)
when a customer is updated (shopify/customers/update)
when a user triggers the task (mechanic/user/trigger)
Options
tag to watch for (required), ignore tag case (boolean), autotag customers after emailing (boolean), email subject (required), email body (required, multiline)
Script
{% assign approval_tag = options.tag_to_watch_for__required %}

{% if options.ignore_tag_case__boolean %}
  {% assign approval_tag = options.tag_to_watch_for__required | downcase %}
{% endif %}

{% capture email_sent_tag %}{{ approval_tag }}-email-sent{% endcapture %}
{% assign tag_processed_metafield_key = task.created_at | date: "%s" | append: approval_tag | append: "tag-processed" | sha256 | slice: 0, 5 | prepend: "-" | prepend: approval_tag %}
{% assign customers_scanned_metafield_key = task.created_at | date: "%s" | append: approval_tag | append: "customers-scanned" | sha256 | slice: 0, 5 | prepend: "-" | prepend: approval_tag %}

{% if event.topic contains "shopify/customers/" %}
  {% assign customer_tags = customer.tags | split: ", " %}

  {% if options.ignore_tag_case__boolean %}
    {% assign customer_tags = customer.tags | downcase | split: ", " %}
  {% endif %}

  {% assign customer_qualifies = true %}
  {% unless customer_tags contains approval_tag %}
    {% assign customer_qualifies = false %}
  {% endunless %}

  {% if options.autotag_customers_after_emailing__boolean %}
    {% if customer_tags contains email_sent_tag %}
      {% assign customer_qualifies = false %}
    {% endif %}
  {% endif %}

  {% if customer.metafields.mechanic[tag_processed_metafield_key] %}
    {% assign customer_qualifies = false %}
  {% endif %}

  {% if event.preview != true %}
    {% if shop.metafields.mechanic[customers_scanned_metafield_key] == nil %}
      {"error": "Please use this task's \"Run task\" button to manually scan your existing customers. Mechanic will take note of everyone who's already tagged, so as to *not* send them an email in the future."}
    {% endif %}
  {% endif %}

  {% if event.preview or customer_qualifies %}
    {% action "email" %}
      {
        "to": {{ customer.email | json }},
        "subject": {{ options.email_subject__required | json }},
        "body": {{ options.email_body__required_multiline | strip | newline_to_br | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    {% endaction %}

    {% action "shopify" %}
      [
        "update",
        ["customer", {{ customer.id | json }}],
        {
          {% if options.autotag_customers_after_emailing__boolean %}
            "tags": {{ customer.tags | add_tag: email_sent_tag | json }},
          {% endif %}
          "metafields": [
            {
              "namespace": "mechanic",
              "key": {{ tag_processed_metafield_key | json }},
              "value_type": "integer",
              "value": "1"
            }
          ]
        }
      ]
    {% endaction %}
  {% endif %}
{% elsif event.topic == "mechanic/user/trigger" %}
  {% if event.preview %}
    {% action "shopify" %}
      [
        "update",
        ["customer", 1234567890],
        {
          "metafields": [
            {
              "namespace": "mechanic",
              "key": {{ tag_processed_metafield_key | json }},
              "value_type": "integer",
              "value": "1"
            }
          ]
        }
      ]
    {% endaction %}

    {% action "shopify" %}
      [
        "create",
        "metafield",
        {
          "namespace": "mechanic",
          "key": {{ customers_scanned_metafield_key | json }},
          "value_type": "integer",
          "value": "1"
        }
      ]
    {% endaction %}
  {% else %}
    {% assign customer_ids_to_mark = array %}
    {% assign cursor = nil %}

    {% for n in (0..100) %}
      {% capture query %}
        query {
          customers(
            first: 250
            query: {{ "tag:" | append: approval_tag | json }}
            after: {{ cursor | json }}
          ) {
            pageInfo {
              hasNextPage
            }
            edges {
              cursor
              node {
                legacyResourceId
                metafield(
                  namespace: "mechanic"
                  key: {{ tag_processed_metafield_key | json }}
                ) {
                  value
                }
              }
            }
          }
        }
      {% endcapture %}

      {% assign result = query | shopify %}
      {% for edge in result.data.customers.edges %}
        {% if edge.node.metafield %}
          {% continue %}
        {% endif %}

        {% assign customer_ids_to_mark[customer_ids_to_mark.size] = edge.node.legacyResourceId %}
      {% endfor %}

      {% if result.data.customers.pageInfo.hasNextPage %}
        {% assign cursor = result.data.customers.edges.last.cursor %}
      {% else %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% for customer_id in customer_ids_to_mark %}
      {% action "shopify" %}
        [
          "update",
          ["customer", {{ customer_id | json }}],
          {
            "metafields": [
              {
                "namespace": "mechanic",
                "key": {{ tag_processed_metafield_key | json }},
                "value_type": "integer",
                "value": "1"
              }
            ]
          }
        ]
      {% endaction %}
    {% endfor %}

    {% action "shopify" %}
      [
        "create",
        "metafield",
        {
          "namespace": "mechanic",
          "key": {{ customers_scanned_metafield_key | json }},
          "value_type": "integer",
          "value": "1"
        }
      ]
    {% endaction %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Tag to watch for
approved
Ignore tag case
true
Email subject
Your account has been approved!
Email body
Hi {{ customer.first_name | default: "there" }},

Your account for {{ shop.name }} has been approved! Thanks for registering, and we'll see you soon.

Thanks,
The team at {{ shop.name }}