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
{% capture newline %}
{% endcapture %}

{% assign csv = "Product ID,Product Title" %}

{% if event.preview %}
  {% capture csv %}{{ csv }}{{ newline }}1234567890,"Example Product"{% endcapture %}
{% else %}
  {% for product in shop.products %}
    {% capture csv %}{{ csv }}{{ newline }}{{ product.id }},{{ product.title | json }}{% endcapture %}
  {% endfor %}
{% endif %}

{
  "action": {
    "type": "email",
    "options": {
      "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 }}
      }
    }
  }
}

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