Email a CSV export of products, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Email a CSV export of products

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 Occurs when a user manually triggers the task. Configuration includes email recipient.

15-day free trial – unlimited tasks

Developer details

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

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
email recipient (email, required)
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more