Mechanic Get Mechanic
Task library

Send a welcome email to new customers, in their language

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

Try this task

When it runs

Occurs whenever a customer is created

What you choose

  • Default locale
  • Email subject translations
  • Email body translations

Documentation

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

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

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

shopify/customers/create
How task subscriptions work

Option types and flags

Task code

{% 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 Liquid reference

Default option values

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 }}"}