Friday, August 5, 2022

Mail Merge with Mozilla Thunderbird [Daily]


Looks like the Mail Merge add-on recently got a major update and revamp - which I could not try out yet; please try it first, and come back here if it still does not work for you.

Mail merging is one of the most demanded, yet less documented "missing" features of Thunderbird (T-Bird); and, surprisingly, one of the most overlooked by all. (Come on, bird, you're an email client... what the ?)

The first search points you to the Mail Merge add-on, which does not work (at least not on the cutting-edge Daily). Beyond that, you start feeling hopeless - rapidly. But luckily, it's not all impossible - not with a few tricks up your sleeve.

T-Bird uses normal file-system based files to store its email content. (They use a MIME-like format, with headers and 80-character-long lines; not sure if it's proprietary, but who cares?) This, among other things, means that you can "inject" custom emails into T-Bird by editing the underlying files. (Luckily their indexing is flexible enough to account for such "spontaneous" emails.)

Let's get to work!

  1. Create a new folder inside T-Bird.
  2. Compose your base email and save it into this folder.
  3. Open your T-Bird profile directory (usually $HOME\AppData\Roaming\Thunderbird\Profiles\{random-id}.default\ (with an extra -nightly suffix, if you use Nightly) on Windows, or $HOME/.thunderbird/{profile-name}/ on Linux (Mac: go figure), where $HOME is your home directory).
  4. Navigate to Mail\Local Folders\ and open the file that has the same name as your T-Bird folder. You'll find the composed email in there.
  5. Make a change in the file and save it. Now if you open the folder again in T-Bird and open the email, you would see the change reflected there.

Now, if you want to mail-merge some template for 3 name-address pairs, you can generate the emails:

  1. Grab your email template.
  2. Replace/format 3 copies of this template using each name and address.
  3. Save the 3 emails into T-Bird's file.
  4. Open the folder in T-Bird.
  5. Select the emails, right click and choose Move to → {your email account} → Drafts.

Now you have 3 nice emails in your Drafts box, ready to be sent out!

But hey, we didn't exactly solve the problem; how do you format those 3 (or 300, or 3000) emails - by hand?

Luckily, scripting can help you do this in less than a minute. I prefer Javascript (JS); just open the [web console] of some browser (Firefox: Ctrl+Shift+K, Chrome: Ctrl+Shift+J), paste the code, press Enter, and paste the result into the T-Bird file!

Let's take an example template:

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hello ${first_name},</p>
    <p>Hope this email finds you well!<br>
    </p>
    <p>This is to inform you that your <a moz-do-not-send="true"
        href="https://some.website.com/">Some Website</a> trial
      period has expired, and we will be disabling your account
      within the next few days.</p>
    <p>Kindly <a moz-do-not-send="true"
href="https://some.website.com/member/payment/">purchase a subscription</a> 
      if you would like to continue using our services.
      You can pick a suitable package from <a moz-do-not-send="true"
        href="https://some.website.com/pricing/">our pricing page</a>.<br>
    </p>
    <p>If you would like to extend your trial instead, feel free to
      reply back to us - so that we can extend your account validity,
      and also offer any additional assistance to set things up.</p>
    <p>Warm Regards,<br>
    </p>
    <pre class="moz-signature" cols="72">-- 
Some Website Support
Some Org Ltd
Some Address, CA 12345
Tel (+1) 12345678 | Email <a class="moz-txt-link-abbreviated"
    href="mailto:info@some-website.com">info@some-website.com</a> | Web <a 
    class="moz-txt-link-abbreviated" href="https://some-website.com">
    some-website.com</a></pre>
  </body>
</html>

If you have the name, address, expiration date and subscription fee in a CSV file (note: there is also a header line):

domain_name,organization,full_name,email
foo.com,Foo Inc,Alice McNamara,alice@foo.com
bar.com,Bar Org,Bob Hennessey,bob@bar.com
baz.com,Baz Corp,John Doe,john@baz.com

This script will generate the emails and copy them into your clipboard (so everything up to the pasting is already done)!

(Make sure to edit the FCC line to match one of your saved templates, and also the From, Subject, etc; also email body - goes without saying.)

data = prompt().split(/\r?\n\r?/).map(s => s.split(/\s+,\s+/));
data.splice(0, 1);
mail = data.map((d, i) => {
	let [domain_name, organization, full_name, email] = d;
	let first_name = full_name.split(' ')[0];
	return `\
From -
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:                                                                                 
FCC: imap://yourname@.some-website.com/[Gmail]/Sent Mail
X-Identity-Key: id3
X-Account-Key: account1
From: Some Website Support <support@some-website.com>
To: ${full_name} <${email}>
Subject: Your Some Website Account is Expiring! [${organization || domain_name}]
Message-ID: <a127b3e4-b1d2-3da0-5806-01fa06fb2dd6@some-website.com>
Date: Tue, 5 May 2020 01:40:19 +0530
X-Mozilla-Draft-Info: internal/draft; vcard=0; receipt=0; DSN=0; uuencode=0;
 attachmentreminder=0; deliveryformat=4
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101
 Thunderbird/74.0a1
MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Language: en-US
Content-Transfer-Encoding: 7bit

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hello ${first_name},</p>
    <p>Hope this email finds you well!<br>
    </p>
    <p>This is to inform you that your <a moz-do-not-send="true"
        href="https://some.website.com/">Some Website</a> trial
      period has expired, and we will be disabling your account
      within the next few days.</p>
    <p>Kindly <a moz-do-not-send="true"
href="https://some.website.com/member/payment/">purchase a subscription</a> 
      if you would like to continue using our services.
      You can pick a suitable package from <a moz-do-not-send="true"
        href="https://some.website.com/pricing/">our pricing page</a>.<br>
    </p>
    <p>If you would like to extend your trial instead, feel free to
      reply back to us - so that we can extend your account validity,
      and also offer any additional assistance to set things up.</p>
    <p>Warm Regards,<br>
    </p>
    <pre class="moz-signature" cols="72">-- 
Some Website Support
Some Org Ltd
Some Address, CA 12345
Tel (+1) 12345678 | Email <a class="moz-txt-link-abbreviated"
    href="mailto:info@some-website.com">info@some-website.com</a> | Web <a 
    class="moz-txt-link-abbreviated"
    href="https://some-website.com">some-website.com</a></pre>
  </body>
</html>`;
});
copy(mail.join('\n\n'));
alert("copied");

Now, if I can just find some time to bundle all this into one simple T-Bird add-on... you won't have to be reading this, at all! (I'll be sure to put that up - in big letters - right at the beginning, if I manage to do so.)

8

No comments: