Email a CSV export of products, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Email a CSV export of products

by Isaac Bowen (team@usemechanic.com)

This task demonstrates Mechanic's ability to compile a CSV export, and send it as an email attachment. In this example, we bundle up a simple export of product titles and IDs, and email it as a CSV attachment.

Runs when a user triggers the task. Configuration includes email recipient.

15-day free trial – unlimited tasks

Documentation

This task demonstrates Mechanic's ability to compile a CSV export, and send it as an email attachment. In this example, we bundle up a simple export of product titles and IDs, and email it as a CSV attachment.

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
email recipient (email, required)
Script
{% if event.preview %}
  {% capture shop_json %}
    {
      "products": [
        {
          "id": 1234567890,
          "title": "Short sleeve t-shirt"
        }
      ]
    }
  {% endcapture %}

  {% assign shop = shop_json | parse_json %}
{% endif %}

{% assign rows = array %}

{% assign header = array %}
{% assign header[0] = "Product ID" %}
{% assign header[1] = "Product Title" %}

{% assign rows[0] = header %}

{% for product in shop.products %}
  {% assign row = array %}
  {% assign row[0] = product.id %}
  {% assign row[1] = product.title %}

  {% assign rows[rows.size] = row %}
{% endfor %}

{% assign csv = rows | csv %}

{% action "email" %}
  {
    "to": {{ options.email_recipient__email_required | json }},
    "subject": {{ "Product ID export for " | append: shop.name | json }},
    "body": "Please see attached. :)",
    "reply_to": {{ shop.customer_email | json }},
    "from_display_name": {{ shop.name | json }},
    "attachments": {
      "export.csv": {{ csv | json }}
    }
  }
{% endaction %}

{% action "echo" csv %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.