tekumatlamallesh

How to Create a mail merge with Gmail & Google Sheets

What are the basic requirements

  • Active Google account. Logged in with Gmail
  • In google drive create one Google spreadsheet
  • Add the script
  • Create a Gmail draft template using spreadsheet placeholders

Steps to follow

First we need to compose a mail using Gmail Draft template.
We need to use the placeholders in the Gmail draft template which we mentioned in the google
sheets spreadsheet.

Each column in the spreadsheet acts as a placeholder tag in the draft.
Whatever placeholders we added in the draft mail script will take those details from the spreadsheet.

How to add the script

Open the spreadsheet.
You can see the list of toolbars in the top.
Click on “Extensions” ->Apps Script

in “Code.gs”

Default you see the few lines of code remove that code and add the below mentioned code.

// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/mail-merge

/*
Copyright 2022 Martin Hawksey

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * @OnlyCurrentDoc
*/

/**
 * Change these to match the column names you are using for email 
 * recipient addresses and email sent column.
*/
const RECIPIENT_COL  = "Employee";
const EMAIL_SENT_COL = "Emailsent";

/** 
 * Creates the menu item "Mail Merge" for user to run scripts on drop-down.
 */
function onOpen() {
  const ui = SpreadsheetApp.getUi();
  ui.createMenu('Mail Merge')
      .addItem('Send Emails', 'sendEmails')
      .addToUi();
}

/**
 * Sends emails from sheet data.
 * @param {string} subjectLine (optional) for the email draft message
 * @param {Sheet} sheet to read data from
*/
function sendEmails(subjectLine, sheet=SpreadsheetApp.getActiveSheet()) {
  // option to skip browser prompt if you want to use this code in other projects
  if (!subjectLine){
    subjectLine = Browser.inputBox("Mail Merge", 
                                      "Type or copy/paste the subject line of the Gmail " +
                                      "draft message you would like to mail merge with:",
                                      Browser.Buttons.OK_CANCEL);

    if (subjectLine === "cancel" || subjectLine == ""){ 
    // If no subject line, finishes up
    return;
    }
  }

  // Gets the draft Gmail message to use as a template
  const emailTemplate = getGmailTemplateFromDrafts_(subjectLine);

  // Gets the data from the passed sheet
  const dataRange = sheet.getDataRange();
  // Fetches displayed values for each row in the Range HT Andrew Roberts 
  // https://mashe.hawksey.info/2020/04/a-bulk-email-mail-merge-with-gmail-and-google-sheets-solution-evolution-using-v8/#comment-187490
  // @see https://developers.google.com/apps-script/reference/spreadsheet/range#getdisplayvalues
  const data = dataRange.getDisplayValues();

  // Assumes row 1 contains our column headings
  const heads = data.shift(); 

  // Gets the index of the column named 'Email Status' (Assumes header names are unique)
  // @see http://ramblings.mcpher.com/Home/excelquirks/gooscript/arrayfunctions
  const emailSentColIdx = heads.indexOf(EMAIL_SENT_COL);

  // Converts 2d array into an object array
  // See https://stackoverflow.com/a/22917499/1027723
  // For a pretty version, see https://mashe.hawksey.info/?p=17869/#comment-184945
  const obj = data.map(r => (heads.reduce((o, k, i) => (o[k] = r[i] || '', o), {})));

  // Creates an array to record sent emails
  const out = [];

  // Loops through all the rows of data
  obj.forEach(function(row, rowIdx){
    // Only sends emails if email_sent cell is blank and not hidden by a filter
    if (row[EMAIL_SENT_COL] == ''){
      try {
        const msgObj = fillInTemplateFromObject_(emailTemplate.message, row);

        // See https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)
        // If you need to send emails with unicode/emoji characters change GmailApp for MailApp
        // Uncomment advanced parameters as needed (see docs for limitations)
        GmailApp.sendEmail(row[RECIPIENT_COL], msgObj.subject, msgObj.text, {
          htmlBody: msgObj.html,
          // bcc: 'a.bbc@email.com',
          // cc: 'a.cc@email.com',
          // from: 'an.alias@email.com',
          // name: 'name of the sender',
          // replyTo: 'a.reply@email.com',
          // noReply: true, // if the email should be sent from a generic no-reply email address (not available to gmail.com users)
          attachments: emailTemplate.attachments,
          inlineImages: emailTemplate.inlineImages
        });
        // Edits cell to record email sent date
        out.push([new Date()]);
      } catch(e) {
        // modify cell to record error
        out.push([e.message]);
      }
    } else {
      out.push([row[EMAIL_SENT_COL]]);
    }
  });

  // Updates the sheet with new data
  sheet.getRange(2, emailSentColIdx+1, out.length).setValues(out);

  /**
   * Get a Gmail draft message by matching the subject line.
   * @param {string} subject_line to search for draft message
   * @return {object} containing the subject, plain and html message body and attachments
  */
  function getGmailTemplateFromDrafts_(subject_line){
    try {
      // get drafts
      const drafts = GmailApp.getDrafts();
      // filter the drafts that match subject line
      const draft = drafts.filter(subjectFilter_(subject_line))[0];
      // get the message object
      const msg = draft.getMessage();

      // Handles inline images and attachments so they can be included in the merge
      // Based on https://stackoverflow.com/a/65813881/1027723
      // Gets all attachments and inline image attachments
      const allInlineImages = draft.getMessage().getAttachments({includeInlineImages: true,includeAttachments:false});
      const attachments = draft.getMessage().getAttachments({includeInlineImages: false});
      const htmlBody = msg.getBody(); 

      // Creates an inline image object with the image name as key 
      // (can't rely on image index as array based on insert order)
      const img_obj = allInlineImages.reduce((obj, i) => (obj[i.getName()] = i, obj) ,{});

      //Regexp searches for all img string positions with cid
      const imgexp = RegExp('<img.*?src="cid:(.*?)".*?alt="(.*?)"[^\>]+>', 'g');
      const matches = [...htmlBody.matchAll(imgexp)];

      //Initiates the allInlineImages object
      const inlineImagesObj = {};
      // built an inlineImagesObj from inline image matches
      matches.forEach(match => inlineImagesObj[match[1]] = img_obj[match[2]]);

      return {message: {subject: subject_line, text: msg.getPlainBody(), html:htmlBody}, 
              attachments: attachments, inlineImages: inlineImagesObj };
    } catch(e) {
      throw new Error("Oops - can't find Gmail draft");
    }

    /**
     * Filter draft objects with the matching subject linemessage by matching the subject line.
     * @param {string} subject_line to search for draft message
     * @return {object} GmailDraft object
    */
    function subjectFilter_(subject_line){
      return function(element) {
        if (element.getMessage().getSubject() === subject_line) {
          return element;
        }
      }
    }
  }

  /**
   * Fill template string with data object
   * @see https://stackoverflow.com/a/378000/1027723
   * @param {string} template string containing {{}} markers which are replaced with data
   * @param {object} data object used to replace {{}} markers
   * @return {object} message replaced with data
  */
  function fillInTemplateFromObject_(template, data) {
    // We have two templates one for plain text and the html body
    // Stringifing the object means we can do a global replace
    let template_string = JSON.stringify(template);

    // Token replacement
    template_string = template_string.replace(/{{[^{}]+}}/g, key => {
      return escapeData_(data[key.replace(/[{}]+/g, "")] || "");
    });
    return  JSON.parse(template_string);
  }

  /**
   * Escape cell data to make JSON safe
   * @see https://stackoverflow.com/a/9204218/1027723
   * @param {string} str to escape JSON special characters from
   * @return {string} escaped string
  */
  function escapeData_(str) {
    return str
      .replace(/[\\]/g, '\\\\')
      .replace(/[\"]/g, '\\\"')
      .replace(/[\/]/g, '\\/')
      .replace(/[\b]/g, '\\b')
      .replace(/[\f]/g, '\\f')
      .replace(/[\n]/g, '\\n')
      .replace(/[\r]/g, '\\r')
      .replace(/[\t]/g, '\\t');
  };
}

After Completing the above steps you can see the “Mail Merge” option shown in the top toolbars section.

Note:

const RECIPIENT_COL = “Employee”;
const EMAIL_SENT_COL = “Emailsent”;

You can change these column names based on your spread sheet.

Steps to Run the script

In your spread sheet create a few columns like “First name” “Last name”
“Employee” “Emailsent”

All columns you will with the information except the “Emailsent” column.
You need to add list of emails in the spreadsheet below any column.

Now from top toolbar section click on “Mail Merge” ->Send Emails option.

After that it will show a pop up box with input field.

Inthat input field you need to mention the Gmail draft template “subject line”

Then it will send the mails to everyone mentioned in the spreadsheet. You can add the images also.

257 Replies to “How to Create a mail merge with Gmail & Google Sheets”

  1. Shining crown online casino əyləncəsi 24/7 mövcuddur.
    Casino shining crown oyunları müasir dizayn ilə fərqlənir.
    Superbet demo shining crown ilə oyun pulsuzdur. Shining crown amusnet çoxlu oyun variantı ilə tanınır. Shining crown buy bonus seçimi əlavə həyəcan yaradır.
    Shining crown jackpot oyunçuların əsas hədəfidir.
    Shining crown slot oyna istənilən cihazdan mümkündür.

    Rəsmi domen buradadır http://shining-crown.com.az/.
    Shining crown big win uduşları oyunçular üçün motivasiya rolunu oynayır.
    Shining crown slot online tez yüklənir.

  2. Sunny coin 2 hold the spin slot free play sınamaq üçün gözəldir. Sunny Coin Hold The Spin onlayn casino seçimləri arasında öndədir.
    Sunny coin 2 hold the spin slot strategiyaları öyrənməyə dəyər. Sunny coin: hold the spin slot oyun təcrübəsi mükəmməldir.
    Rəsmi platforma https://sunny-coin.com.az.
    Sunny coin hold the spin demo oyna, risk olmadan öyrən. Sunny coin: hold the spin slot şanslı oyunçular üçün əladır. Sunny Coin Hold The Spin slot oyunu çoxlu bonus təklif edir.
    Sunny Coin Hold The Spin slot oyunu sürprizlərlə doludur. Sunny Coin Hold The Spin oyunu hər büdcəyə uyğundur.

  3. Bakı Formula 1 biletləri ilə bağlı ən sərfəli təklifləri araşdırın. Formula 1 üçün ən yaxşı bilet seçimlərini tapmaq mümkündür.

    Rəsmi bilet sifarişi üçün klikləyin ➡ formula 1 baku tickets.
    Formula 1 schedule daim yenilənir və asan izlənilir. Formula 1 Bakı 2024 biletləri onlayn əldə oluna bilər.
    Formula 1 takvimi hər mövsüm yenilənir. Formula 1 Azərbaycan mərhələsinin atmosferi barədə şərhlər. Formula 1 izləyiciləri üçün ən son xəbərlər. Formula 1 canlı izləmə platformaları haqqında təcrübələr.

  4. Mobil cihazlarda soccer oyunu yüksək keyfiyyətlə işləyir.
    World soccer champs apk futbol həvəskarlarının seçimi olub. Ən son versiyanı yükləmək üçün world soccer champs apk linkinə keçid edin.
    Super liquid soccer fərqli futbol təcrübəsi bəxş edir. Dream league soccer 2019 hələ də sevilən versiyalardandır. Bubble soccer dostlarla əyləncəli vaxt keçirməyə imkan verir.
    Soccer stream vasitəsilə canlı oyun izləmək mümkündür. Soccer manager 2025 hile ilə əlavə imkanlar mövcuddur. Pro evolution soccer 2019 hələ də çox oynanır. World soccer champs hile son sürüm oyunçuların seçimi olub.

  5. Canlı nəticələr və LaLiga puan durumu bir yerdə. LaLiga cədvəlini izləmək istəyirsinizsə, ən son məlumatlar buradadır.
    İspaniya LaLiga canlı futbolunu qaçırmayın. Yeni mövsüm üçün LaLiga logo 2025 təqdim edildi.
    Futbol azarkeşləri üçün etibarlı mənbə — LaLiga standings. Komandaların xal durumu və nəticələri hər gün təzələnir.
    LaLiga goal of the season 2022 hələ də yadda qalır.
    LaLiga maçlar və canlı yayım üçün bələdçi.
    LaLiga statistika səhifəsində komanda göstəriciləri.
    LaLiga champions 2023 komandaları arasında böyük rəqabət.
    Spaniyada futbolun nəbzi LaLiga ilə vurur.

  6. Лука Модрич азыркы замандын эң мыкты оюнчуларынын бири.Модрич кетсе дагы, анын izi футболдо калат. Модричтин оюн көрсөткүчтөрү жаш оюнчуларга үлгү. Лука Модричтин жашы 39 болсо да, оюну жаштардыкындай. Реалдагы доору, ЛЧ финалдары жана рекорддор — баары ушул жакта: https://luka-modric-kg.com/. Лука Модрич дүйнөлүк футболго башкача дем берди.

    Модрич Реалдын орто талаасын башкарып келет көп жылдан бери. Анын автобиографиясы мотивация берет. Модрич акчадан мурда сыймыкты ойлойт. Футбол дүйнөсү Модричсиз элестетүү кыйын.

  7. Чыныгы мотивация издеген адам Алекс Перейранын жолун көрсүн. Көрсө, Алекс Перейра көчөдөн чыккан, эми болсо дүйнөнүн чокусунда. UFC фанаттары үчүн пайдалуу булак — alex-pereira-kg.com. Бул жигитти “камень” деп бекер айтпайт.
    Перейранын жашоосу — мотивация.
    Перейра дайыма өзүн жакшыртып келет. Анын мотивациялык видеолору чындап шыктандырат. Ким болбосун, Перейраны жеңүү кыйын.
    Ар бир фанат анын күчүн сезет. Эгер спортту сүйсөң, Перейранын окуясын оку.

  8. Флойд Мейвезер ар бир беттешинде жогорку деңгээлди көрсөтөт. Флойд Мейвезердин акыркы жаңылыктары ар бир күйөрманды кызыктырат. Флойд Мейвезердин карьерасы жана акыркы жаңылыктары тууралуу көбүрөөк маалыматты Флойд Мейвезер сайтынан таба аласыз.
    Флойд Мейвезер ар дайым спорттун чокусунда болгон. Флойд Мейвезер ар бир беттешинде тактиканы мыкты колдонот. Анын акыркы жаңылыктарын укпай калган адам жок.
    Флойд Мейвезер ар бир кадамын ойлонуп жасайт.
    Флойд Мейвезердин рекорддору таң калтырат. Анын тарыхы ар дайым спорт күйөрмандарынын эсинде. Флойд Мейвезер – күч, ылдамдык жана акылдын айкалышы.

  9. Кейндин упай топтоо жөндөмү легендардуу.Бул оюнчу дайыма эң мыктылардын арасында. Кейндин күйөрмандары үчүн мыкты маалымат булагы — Гарри Кейн профили. Гарри Кейн дайыма өз командасына ишеним жаратат.
    Гарри Кейн Германияда да өз деңгээлин көрсөттү.
    Гарри Кейндин балдары футболду жакшы көрүшөт. Гол киргизүүдө ал өзгөчө инстинктке ээ. Анын ар бир голу эстен чыкпайт. Гарри Кейндин балалык сүрөттөрү интернетте көп. Бул оюнчу кайсы чемпионатта болбосун айырмаланат. Кейндин трансферлери ар дайым чоң темалар.

  10. Түз эфирге чейин негизги фактылар менен тааныштырып, беттештен соң жыйынтык талдоону чыгарабыз.
    Пресс-конференция жана тарсылдатуу жыйынтык видеолору бет ачылгандан кийин эле жеткиликтүү. ислам махачев рекорд Жеңиш/жеңилүү тарыхы жана финиш проценттери дайыма жаңыртылып турат. Диаграммалар кошулган. Алгачкы беттештеги негизги эпизоддорду эске салып, реванш үчүн сабактарды белгилеп беребиз. Илия Топурия vs Ислам Махачев айкашы жөнүндө ушак-айың эмес, текшерилген булактар гана берилет. Мурдагы атаандаштарынын стили менен салыштыруу аркылуу контекст берилет.
    Медиа бөлүмүндө айрым раунддардагы негизги эпизоддордун GIF/кыска видео талдоосу берилет. Оппоненттердин акыркы үч беттешиндеги формасын салыштырып, өңүт көрсөтөбүз. Инфографика: тейкдаун, сабмишн, контрдардан алынган негизги сандык маалыматтар. Материалдар нейтралдуу, деңгээлдүү жана фанаттарга да, жаңы окурмандарга да ылайык.

  11. Неймар в «Сантосе» — золотая эпоха: сверяю статистику 2011–2012 и пересматриваю игры в canlı izləmə. Интересует «неймар рост вес возраст» по годам — удобнее видеть в одной инфографике. Активно обновляю коллекцию заставок для телефона, ищу чистые 4K. http://www.neymar-kg.com. Ищу «неймар красивые фото 2024» в стиле editorial — подскажите, какие фотосеты топ.
    «Неймар золотой мяч» — почему не выиграл? Интересны аналитические статьи без фанатизма. Нужны «обои на телефон футбол Неймар» в вертикальном формате 1080×2400. Неймар толстый — ещё один миф: интересен анализ формы после травм и пауз. Неймар Барселона обои — хочется классики без кричащих фильтров и водяных знаков.
    Неймар какой клуб сейчас — нужен снэпшот для карточек игроков. Неймар обои на телефон — тёмные темы помогают экономить батарею на OLED.

  12. Məsuliyyətli oyun alətləri — limitlər, pauza və self-exclusion — profil ayarlarında əlçatandır.
    Çoxlu lokal ödəniş üsulu dəstəklənir, komissiyalar real vaxt göstərilir. Yeni başlayanlar qeydiyyatdan sonra pinco casino az təkliflərini yoxlaya və demo rejimdə oyunlarla tanış ola bilər. Çıxarışlar e-cüzdana adətən bir neçə dəqiqə, kartlara isə bankdan asılı olaraq daha uzun çəkir. Provayder səhifələrində kolleksiya və paylama tezliyi haqda icmallar var.
    Yeni oyunçular üçün təlimat məqalələri sadə dildə yazılıb, ekran görüntüləri kömək edir. Aşağı internet sürətində də yüngül rejim avtomatik aktivləşir.
    Messencer kanallarına keçidlər rəsmi səhifədə yerləşir. Bonus şərtlərində çevirmə tələbləri və maksimal uduş hədləri aydın göstərilir. Sürətli ödəniş axını və şəffaf limitlər rahat təcrübə yaradır.

  13. Роберт Левандовски акыркы оюндарында мыкты оюн көрсөттү.
    Левандовскинин жашоо образы көптөр үчүн мотивация. Левандовски канча жыл ойногонуна карабай, деңгээлин түшүргөн жок. Левандовски өз өлкөсүнүн сыймыгы. Барселона жана Левандовски тууралуу кеңири маалымат https://robert-lewandowski-kg.com/ порталында бар. Левандовски Барселонада өзүнүн тажрыйбасын көрсөтүүдө.
    Левандовскинин биографиясы шыктануу жаратат. Левандовски жаракаттан кийин дагы күчтүү кайтып келген.
    Роберт Левандовскинин үй-бүлөлүк тамыры Польшада. Роберт Левандовски ар дайым эмгектин баасын билет.

  14. В этом информативном обзоре собраны самые интересные статистические данные и факты, которые помогут лучше понять текущие тренды. Мы представим вам цифры и графики, которые иллюстрируют, как развиваются различные сферы жизни. Эта информация станет отличной основой для глубокого анализа и принятия обоснованных решений.
    Получить больше информации – https://vivod-iz-zapoya-1.ru/

Leave a Reply to Malcolm Muessig Cancel reply

Your email address will not be published. Required fields are marked *