Send an email to all customers, with Mechanic.

Mechanic is a development platform for Shopify. :)

Send an email to all customers

by Isaac Bowen (team@usemechanic.com)

Use this task to easily contact all of your customers at once, optionally filtering by customer tag. Important: This task can only be used in a transactional context - it cannot be used be used for marketing emails, or any type of message that would require an unsubscribe link.

Runs when a user triggers the task. Configuration includes only send to customers having this tag, email subject, email body, and test mode.

15-day free trial – unlimited tasks

Documentation

This task will send your preconfigured email to all of your customers, optionally filtering by preconfigured tag. Make sure to double-check your task configuration before sending! Use test mode to receive a list of customers who will receive your email, before running without it.

Important: This task can only be used in a transactional context - it cannot be used be used for marketing emails, or any type of message that would require an unsubscribe link. Read more: What kind of email can I send with Mechanic?

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 user triggers the task (mechanic/user/trigger)
Options
only send to customers having this tag, email subject (required), email body (required, multiline), test mode (boolean)
Script
{% comment %}
  Express an opinion about option order

  {{ options.only_send_to_customers_having_this_tag }}
  {{ options.email_subject__required }}
  {{ options.email_body__required_multiline }}
  {{ options.test_mode__boolean }}
{% endcomment %}

{% assign cursor = nil %}
{% assign emails = array %}

{% for n in (0..100) %}
  {% capture query %}
    query {
      customers(
        first: 250
        after: {{ cursor | json }}
        {% if options.only_send_to_customers_having_this_tag != blank %}
          query: {{ options.only_send_to_customers_having_this_tag | json | prepend: "tag:" | json }}
        {% endif %}
      ) {
        pageInfo {
          hasNextPage
        }
        edges {
          cursor
          node {
            email
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "customers": {
            "edges": [
              {
                "node": {
                  "email": "customer@example.com"
                }
              }
            ]
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% for customer_edge in result.data.customers.edges %}
    {% assign customer = customer_edge.node %}
    {% if customer.email != blank %}
      {% assign emails[emails.size] = customer.email %}
    {% endif %}
  {% endfor %}

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

{% if options.test_mode__boolean %}
  {% action "echo" emails %}
{% else %}
  {% for email in emails %}
    {% action "email" %}
      {
        "to": {{ email | json }},
        "subject": {{ options.email_subject__required | strip | 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 %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Test mode
true