title: Pulling Gmail "from:mailer-damon" and "from:postmaster" email data into google sheets?
body: <p>I came across the code below in an article. I tried to modify it (<code>q: "from:mailer-daemon OR from:postmaster",</code>) so that the code can pull from not only <code>mailer-daemon</code>, but also <code>postmaster</code> emails. However, it handled the data differently for <code>postmaster</code> emails (<code>postmaster</code> recipient emails were put into the "Bounce Reason" column along with the reason). I'd also like for it to pull from a label I've created in Gmail called "Delete from CMMC List", but I'm unsure how to do that.</p>
  
  <p>The column names that data is being separated into are as follows:</p>
  
  <pre><code>Bounce Date
  Email Recipient
  Error Status
  Bounce Reason
  Gmail Link
  </code></pre>
  
  <p>Original code that needs modification:</p>
  
  <pre><code>/*
  * Gmail Bounce Report
  * u/labnol Feb 12, 2020
  * Written by Amit Agarwal
  * email: amit@labnol.org
  * twitter: u/labnol
  * web: www.labnol.org
  */
  
  const toast_ = e =&gt; SpreadsheetApp.getActiveSpreadsheet().toast(e);
  
  const parseMessage_ = messageId =&gt; {
      const message = GmailApp.getMessageById(messageId);
      const body = message.getPlainBody();
      const [, failAction] = body.match(/^Action:\s*(.+)/m) || [];
  
      if (failAction === "failed") {
          const emailAddress = message.getHeader("X-Failed-Recipients");
          const [, errorStatus] = body.match(/^Status:\s*([.\d]+)/m) || [];
          const [, , bounceReason] =
              body.match(/^Diagnostic-Code:\s*(.+)\s*;\s*(.+)/m) || [];
  
          if (errorStatus &amp;&amp; bounceReason) {
              return [
                  message.getDate(),
                  emailAddress,
                  errorStatus,
                  bounceReason.replace(/\s*(Please|Learn|See).+$/, ""),
                  `=HYPERLINK("${message.getThread().getPermalink()}";"View")`
              ];
          }
      }
      return false;
  };
  
  const writeBouncedEmails_ = (data = []) =&gt; {
      if (data.length &gt; 0) {
          toast_("Writing data to Google Sheet..");
          const sheet = SpreadsheetApp.getActiveSheet();
  
          sheet
              .getRange(3, 1, sheet.getLastRow(), sheet.getLastColumn())
              .clearContent();
          sheet.getRange(3, 1, data.length, data[0].length).setValues(data);
  
          SpreadsheetApp.flush();
          toast_("Bounce Report is ready!");
          showCredit_();
      }
  };
  
  const findBouncedEmails_ = () =&gt; {
      try {
          const rows = [];
          const { messages = [] } = Gmail.Users.Messages.list("me", {
              q: "from:mailer-daemon",
              maxResults: 200
          });
  
          if (messages.length === 0) {
              toast_("No bounced emails found in your mailbox!");
              return;
          }
  
          toast_("Working..");
  
          for (let m = 0; m &lt; messages.length; m += 1) {
              const bounceData = parseMessage_(messages[m].id);
  
              if (bounceData) {
                  rows.push(bounceData);
              }
  
              if (rows.length % 10 === 0) {
                  toast_(`${rows.length} bounced emails found so far..`);
              }
          }
          writeBouncedEmails_(rows);
      } catch (error) {
          toast_(error.toString());
      }
  };
  
  const onOpen = e =&gt; {
      SpreadsheetApp.getUi()
          .createMenu("‍♂️ Bounced Emails")
          .addItem("Run Report", "findBouncedEmails_")
          .addSeparator()
          .addItem("Credits", "showCredit_")
          .addToUi();
  };
  
  const showCredit_ = () =&gt; {
      const template = HtmlService.createHtmlOutputFromFile("help");
      const html = template
          .setTitle("Bounce Report for Gmail")
          .setWidth(460)
          .setHeight(225);
      SpreadsheetApp.getActiveSpreadsheet().show(html);
  }
  </code></pre>