---
title: "r/GMail &#8211; Has anybody here tried to improve Martin Hawksey&#8217;s free Mail Merge system? [adding custom attachments to mailing] "
author: "Martin Hawksey"
date: 2021-12-09T22:07:19+00:00
modified: 2021-12-09T22:10:28+00:00
canonical_url: https://pulse.appsscript.info/p/2021/12/r-gmail-has-anybody-here-tried-to-improve-martin-hawkseys-free-mail-merge-system-adding-custom-attachments-to-mailing/
markdown_url: https://pulse.appsscript.info/p/2021/12/r-gmail-has-anybody-here-tried-to-improve-martin-hawkseys-free-mail-merge-system-adding-custom-attachments-to-mailing.md
categories:
  - "Beginner"
  - "Comment"
  - "Gmail"
  - "Sheets"
  - "Solutions"
  - "V8"
description: "I'm not really a reddit user but this question about adding custom attachments my Google Workspace Solutions Gallery - Mail Merge example  landed in my inbox. The solution is, in Google Apps Script tradition, one line of code!"
---

# r/GMail &#8211; Has anybody here tried to improve Martin Hawksey&#8217;s free Mail Merge system? [adding custom attachments to mailing] 

> [Has anybody here tried to improve Martin Hawksey's free Mail Merge system?](https://www.reddit.com/r/GMail/comments/rb2pxj/has_anybody_here_tried_to_improve_martin_hawkseys/) by in[GMail](https://www.reddit.com/r/GMail/)

I’m not really a reddit user but this question about adding custom attachments my [Google Workspace Solutions Gallery – Mail Merge**](https://developers.google.com/workspace/solutions/mail-merge) example landed in my inbox. The solution is, in Google Apps Script tradition, one line of code! The line is:

`if(row['Attachment'] != '') emailTemplate.attachments.push(DriveApp.getFileById(row['Attachment']).getBlob());`

The logic is:

1. check if the row value in the column named **Attachment **
2. if true call Drive to open file with attachment id and push to the `emailTemplate` object.

The line is added just after:

`const msgObj = fillInTemplateFromObject_(emailTemplate.message, row);`

If you prefer you can also [copy this sheet for a working example**](https://docs.google.com/spreadsheets/d/1n_M_obCnHxXzWgVnUvTNwyunQiMsO6b1ZiMX4fYVIYc/copy). To attach different files the **Attachment **row cell needs to include a Google Drive file id. There are a number of ways you can get Drive file id’s. For example, here is a [Getting all files’ file-id from a folder in Google Drive snippet**](https://webapps.stackexchange.com/a/86881/30021).

If you are interested in more of what is happening in this solution, the `emailTemplate` object is returned by `getGmailTemplateFromDrafts_()`. This function gets the Gmail draft message and as well as any attachments including inline images. If you are interested in handling attachments and inline images you can read more in [Everything you ever wanted to know about Gmail draft inline images and Google Apps Script but were afraid to ask**](https://hawksey.info/blog/2021/02/everything-you-ever-wanted-to-know-about-gmail-draft-inline-images-and-google-apps-script-but-were-afraid-to-ask/).

---

*Published by [Martin Hawksey](https://pulse.appsscript.info/p/author/admin/) on [Pulse](https://pulse.appsscript.info/). Original article: [https://pulse.appsscript.info/p/2021/12/r-gmail-has-anybody-here-tried-to-improve-martin-hawkseys-free-mail-merge-system-adding-custom-attachments-to-mailing/](https://pulse.appsscript.info/p/2021/12/r-gmail-has-anybody-here-tried-to-improve-martin-hawkseys-free-mail-merge-system-adding-custom-attachments-to-mailing/)*
