Trigger order emails with a tag, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Trigger order emails with a tag

by Isaac Bowen (team@usemechanic.com)

This task monitors order tags, and sends out an email that you configure whenever a certain tag is added. Supports email templates, and sending to custom recipients.

Runs when an order is updated. Configuration includes email trigger tag, email sent tag, email subject, and email body html.

15-day free trial – unlimited tasks

Documentation

This task monitors order tags, and sends out an email that you configure whenever a certain tag is added.

By default, this email will be sent to the address on file for the order. However, you can specify a custom recipient by tagging the order with "email:custom@example.com". (This example assumes you use "email" as your email trigger tag.)

When this task sends out an email, it removes the tag that triggered the message, replacing it with the email used for delivery, prefixed with the "email sent" tag.

Feel free to tag the order with multiple trigger tags at once, e.g. "email", "email:customer@example.com", "email:sales@example.com". After processing, Mechanic will leave the task tagged "sent", "sent:customer@example.com:sent", "sent:sales@example", leaving any and all other tags untouched.

Please note: Shopify restricts order tags to 40 characters in length.

To re-use your store's confirmation email template, or to use any other email template, configure your email template with Mechanic, and add the Mechanic email template name to your task.

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 an order is updated (shopify/orders/updated)
Options
email trigger tag (required), email sent tag (required), email subject (required), email body html (required, multiline, code)
Script
{% if event.preview %}
  {% assign order = '{"admin_graphql_api_id":"gid://shopify/Order/1234567890"}' | parse_json %}
  {% assign order["email"] = "customer@example.com" %}
  {% assign order["tags"] = options.email_trigger_tag__required %}
{% endif %}

{% assign email_recipient = order.email %}
{% assign email_trigger_root = options.email_trigger_tag__required %}
{% assign email_trigger_root_to_match = email_trigger_root | downcase %}
{% assign email_sent_root = options.email_sent_tag__required %}
{% assign email_sent_root_to_match = email_sent_root | downcase %}

{% assign email_recipients = array %}
{% assign order_tags_to_add = array %}
{% assign order_tags_to_remove = array %}

{% assign order_tags = order.tags | downcase | split: ", " %}

{% for order_tag in order_tags %}
  {% assign new_email_recipient = nil %}

  {% assign parts = order_tag | downcase | split: ":" %}
  {% if parts[0] == email_trigger_root_to_match %}
    {% if parts.size == 1 %}
      {% assign new_email_recipient = order.email %}
    {% elsif parts[1] contains "@" %}
      {% assign new_email_recipient = parts[1] %}
    {% endif %}
  {% endif %}

  {% if new_email_recipient != blank %}
    {% assign email_recipients[email_recipients.size] = new_email_recipient %}
    {% assign order_tags_to_remove[order_tags_to_remove.size] = order_tag %}
    {% assign order_tags_to_add[order_tags_to_add.size] = email_sent_root | append: ":" | append: new_email_recipient %}
  {% endif %}
{% endfor %}

{% for email_recipient in email_recipients %}
  {% action "email" %}
    {
      "to": {{ email_recipient | json }},
      "subject": {{ options.email_subject__required | json }},
      "body": {{ options.email_body_html__required_multiline_code | strip | json }},
      "reply_to": {{ shop.customer_email | json }},
      "from_display_name": {{ shop.name | json }}
    }
  {% endaction %}
{% endfor %}

{% if order_tags_to_add != blank or order_tags_to_remove != blank %}
  {% action "shopify" %}
    mutation {
      {% if order_tags_to_add != blank %}
        tagsAdd(
          id: {{ order.admin_graphql_api_id | json }}
          tags: {{ order_tags_to_add | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      {% endif %}

      {% if order_tags_to_remove != blank %}
        tagsRemove(
          id: {{ order.admin_graphql_api_id | json }}
          tags: {{ order_tags_to_remove | json }}
        ) {
          userErrors {
            field
            message
          }
        }
      {% endif %}
    }
  {% endaction %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Email trigger tag
email
Email sent tag
sent
Email subject
About your order: {{ order.name }}