Email all customers who recently purchased a certain product with Mechanic.

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

Email all customers who recently purchased a certain product

by Isaac Bowen (team@usemechanic.com)

Need to reach customers who purchased something specific? This task scans your order history over the last 60 days, looking for paid orders for the product you specify, and sends each one an email that you can customize.

Runs when some text is submitted. Configuration includes email subject and email body.

15-day free trial – unlimited tasks

Documentation

Enter the product ID (i.e. 1234567890), and Mechanic will email everyone who purchased it, filtering for authorized orders from the last 60 days.

Optionally, add a space followed by a numeric price to filter for only orders that have exactly that total price. For example, enter 1234567890 100.00 to email customers who ordered product 1234567890, but only if their order total was exactly $100.

Use FIRST_NAME and ORDER_NUMBER in the email body to greet the customer by name, with their order number.

Developer details

Events
when some text is submitted (mechanic/user/text)
Options
email subject (required), email body (required, multiline)
Script
{% if event.preview %}
  {
    "action": {
      "type": "email",
      "options": {
        "to": "customer@example.com",
        "subject": {{ options.email_subject__required | strip | json }},
        "body": {{ options.email_body__required_multiline | replace: "FIRST_NAME", "John" | replace: "ORDER_NUMBER", "12345" | strip | newline_to_br | json }},
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    }
  }
{% else %}
  {% assign arguments = event.data | split: " " %}
  {% assign target_product_id = arguments[0] | plus: 0 %}
  {% assign target_order_total_price = nil %}
  {% if arguments.size > 1 %}
    {% assign target_order_total_price = arguments[1] %}
  {% endif %}

  {% for order in shop.orders.paid %}
    {% if order.email == blank %}
      {% continue %}
    {% endif %}

    {% assign product_ids = order.line_items | map: "product_id" %}
    {% unless product_ids contains target_product_id %}
      {% continue %}
    {% endunless %}

    {% if target_order_total_price != nil and order.total_price != target_order_total_price %}
      {% continue %}
    {% endif %}

    {
      "action": {
        "type": "email",
        "options": {
          "to": {{ order.email | json }},
          "subject": {{ options.email_subject__required | strip | json }},
          "body": {{ options.email_body__required_multiline | replace: "FIRST_NAME", order.customer.first_name | replace: "ORDER_NUMBER", order.order_number | strip | newline_to_br | json }},
          "reply_to": {{ shop.customer_email | json }},
          "from_display_name": {{ shop.name | json }}
        }
      }
    }
  {% endfor %}
{% endif %}
Yep, that’s just Liquid! If this is interesting to you, check out our documentation! Mechanic is hugely developer-friendly. :)