Send a welcome email to new customers, in their language, with Mechanic.

Mechanic is a development platform for Shopify. :)

Send a welcome email to new customers, in their language

by Isaac Bowen (team@usemechanic.com)

This task demonstrates how to send emails that respect the customer's language preference, by matching their locale to a pre-written email translation.

Runs when a customer is created. Configuration includes default locale, email subject translations, and email body translations.

15-day free trial – unlimited tasks

Documentation

For more on the techniques in this task, see Sending multilingual emails.

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 customer is created (shopify/customers/create)
Options
default locale (required), email subject translations (keyval, required), email body translations (keyval, multiline, required)
Script
{% assign default_locale = options.default_locale__required %}

{% if options.email_subject_translations__keyval_required[default_locale] == blank %}
  {% error "Please add an email subject for your default locale." %}
{% elsif options.email_body_translations__keyval_multiline_required[default_locale] == blank %}
  {% error "Please add an email body for your default locale." %}
{% endif %}

{% capture query %}
  query {
    customer(id: {{ customer.admin_graphql_api_id | json }}) {
      locale
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "customer": {
          "locale": {{ options.email_subject_translations__keyval_required.first.first | json }}
        }
      }
    }
  {% endcapture %}

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

{% assign locale = result.data.customer.locale %}

{% assign email_subject = options.email_subject_translations__keyval_required[locale] %}
{% assign email_body = options.email_body_translations__keyval_multiline_required[locale] %}

{% if email_subject == blank %}
  {% log message: "Couldn't find an email subject for this locale; falling back to the default", locale: locale %}
  {% assign email_subject = options.email_subject_translations__keyval_required[default_locale] %}
{% endif %}

{% if email_body == blank %}
  {% log message: "Couldn't find an email subject for this locale; falling back to the default", locale: locale %}
  {% assign email_body = options.email_body_translations__keyval_multiline_required[locale] %}
{% endif %}

{% action "email" %}
  {
    "to": {{ customer.email | json }},
    "subject": {{ email_subject | json }},
    "body": {{ email_body | newline_to_br | json }},
    "reply_to": {{ shop.customer_email | json }},
    "from_display_name": {{ shop.name | json }}
  }
{% endaction %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Default locale
es
Email subject translations
{"es"=>"¡Bienvenidos, {{ customer.first_name }}!", "en"=>"Welcome, {{ customer.first_name }}!"}
Email body translations
{"es"=>"Hola {{ customer.first_name }},\n\nEstamos contentos de que estés aquí. :) ¡Salud!\n\n{{ shop.name }}", "en"=>"Hello {{ customer.first_name }},\n\nWe're glad you're here. :) Cheers!\n\n{{ shop.name }}"}