DEV Community

Send Emails from Gmail to Discord Channel

0xkoji on January 25, 2022

I created a Discord server for an alumni’s mailing list since many people asked “Do we have a Slack group or a Discord server?” and there wouldn’t ...
Collapse
 
0xkoji profile image
0xkoji

The code is already in this post. (Line 4)

Collapse
 
thomasseandominickelly profile image
Thomas Sean Dominic Kelly

Thanks for this, it is really useful and lightweight.

I've got a company account which only rarely receives emails, and I am always forgetting to check it. I have rejigged this code slightly so that it only checks for unread messages and forwards those to the specified discord channel (I've made a private channel only accessible to me on the server). Finally, it marks these messages as read so that you don't get repeat messages. Moving my email notification system to discord 🙂

function postDiscord(postMsg) {
  const props = PropertiesService.getScriptProperties();
  const webhooks = props.getProperty("WEBHOOKS"); // get value from project properties
  const token = props.getProperty("TOKEN");
  const channel = 'jobs' // channel name
  const parse = 'full';
  const method = 'post';

  const payload = {
    'token': token,
    'channel': channel,
    'content': postMsg,
    'parse': parse,
  };

  const params = {
    'method': method,
    'payload': payload,
    'muteHttpExceptions': true,
  };
  response = UrlFetchApp.fetch(webhooks, params);
}

function sendMailsToDiscord() {
  const unreadCount = GmailApp.getInboxUnreadCount();

  if(unreadCount === 0) {
    return;
  }

  const threads = GmailApp.getInboxThreads(0, unreadCount);
  const msgs = GmailApp.getMessagesForThreads(threads);
  for(let i =0; i<msgs.length; i++) {

    for(let j =0; j<msgs[i].length; j++) {
      const msg = msgs[i][j];
      const msgFrom = msg.getFrom();
      const msgBody = msg.getPlainBody();
      const msgSubject = msg.getSubject();

      const postMsg = 
      `From: ${msgFrom} \nTitle: ${msgSubject} \nBody: ${msgBody}`

      // The limit is 2000 characters
      if(postMsg.length > 2000) {
        const stopPos = 1900; // 
        const errorMsg =  "`This message is more than 2000 chars so I cannot post the entire message. Sorry.`";
        postMsg = postMsg.substring(0, stopPos) + "\n" + errorMsg
      }
      console.log(postMsg);
      console.log('===================================');
      console.log(`chars: ${postMsg.length}`);
      console.log('===================================');
      postDiscord(postMsg);
      GmailApp.markMessageRead(msg);
    }

  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
southvictor336 profile image
SouthVictor336

Does this work? How will the script find the email without a search Query?

Collapse
 
thomasseandominickelly profile image
Thomas Sean Dominic Kelly

It does indeed work, it has been running since this post in fact.

The script doesn't need to search for emails, as it just grabs the all the unread emails, forwards them to discord and finally marks them as read, so you don't get repeat notifications.

It figures out what emails are unread with this line:

  const unreadCount = GmailApp.getInboxUnreadCount();
Enter fullscreen mode Exit fullscreen mode

However, there is a bug in the code... can you see it?


postMsg is defined as a const but is reassigned if the email message is longer than 2000. That is the only change I have had to make to the script

Collapse
 
frankylee profile image
Frank Lee

Does this script still work? I've been trying to get it to work but nothing is happening. I believe I've entered in the correct webhook/token as I'm not getting any error messages when running the script. I'm seeing Execution started and Execution completed.

I've only changed two things in the the script. The channel name and the searchQuery, but it yields nothing. I also double checked that the webhook has permission to post in the channel. Any ideas to what I'm doing wrong?

const searchQuery = 'subject:Your collection day is tomorrow';

Enter fullscreen mode Exit fullscreen mode
Collapse
 
frankylee profile image
Frank Lee

Nvm, I figured out what I was doing wrong. I wasn't pushing the correct command. Thanks again for the script as it works well!

Collapse
 
0xkoji profile image
0xkoji

Nice, glad to hear that!
I'm thinking of writing a new post about this since I've received some comments.

Collapse
 
evolze profile image
Evolze • Edited

There seems to be an issue or it does not work anymore 😥 I followed the instructions (great guide btw) and when I run the script, I get the following error. Any ideas on what I'm doing wrong?

*Error Exception: *
DNS error: 1079268134118248508
postDiscord @ Code.gs:21
sendMailsToDiscord @ Code.gs:60

Here's the modification I made to the script:

function postDiscord(postMsg) {
  const props = PropertiesService.getScriptProperties();
  const webhooks = props.getProperty("WEBHOOKS"); // get value from project properties
  const token = props.getProperty("TOKEN");
  const channel = 'unraid-notifications' // channel name
  const parse = 'full';
  const method = 'post';

  const payload = {
    'token': token,
    'channel': channel,
    'content': postMsg,
    'parse': parse,
  };

  const params = {
    'method': method,
    'payload': payload,
    'muteHttpExceptions': true,
  };
  response = UrlFetchApp.fetch(webhooks, params); // First Error Message Points Here
}

function sendMailsToDiscord() {
  const searchQuery = 'from:realemailhere@gmail.com';
  const dt = new Date();
  const checkSpan = 30;
  dt.setMinutes(dt.getMinutes() - checkSpan);

  const threads = GmailApp.search(searchQuery);
  const msgs = GmailApp.getMessagesForThreads(threads);
  for(let i =0; i<msgs.length; i++) {
    const lastMsgDt = threads[i].getLastMessageDate();

    if(lastMsgDt.getTime() < dt.getTime()) {
      break;
    }

    for(let j =0; j<msgs[i].length; j++) {
      const msgDate = msgs[i][j].getDate();
      // const msgBody = msgs[i][j].getBody(); // html
      const msgBody = msgs[i][j].getPlainBody();
      const subject = msgs[i][j].getSubject()
      const postMsg = "Backups successful" + "\n" +
          Utilities.formatDate(msgDate, 'America/Denver', 'MM/DD/yyyy hh:mm:ss') + "\n" +
              "Title:" + subject + "\n" +
              "[hr]" +
               msgBody;
      console.log(`chars: ${postMsg.length}`);
      // The limit is 2000 characters
      if(postMsg.length > 2000) {
        const stopPos = 1900; // 
        const msg =  "`This message is more than 2000 chars so I cannot post the entire message. Sorry.`";
        postMsg = postMsg.substring(0, stopPos) + "\n" + msg
      }
      console.log(postMsg);
      console.log('===================================');
      console.log(`chars: ${postMsg.length}`);
      console.log('===================================');
      postDiscord(postMsg); // Second Error Message Points to Here
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
0xkoji profile image
0xkoji

The script itself still works as expected on my end.
Are you sure that you set the webhook url and token properly and pass the proper params?

Collapse
 
evolze profile image
Evolze • Edited

Yeah I double checked. The 'classic' version of the properties pane does not seem to be available anymore. Here is what I have on my end when I go to the left-hand menu bar (Project Settings --> Script Properties):

Image description

For the script properties, am I supposed to provide a URL or how is it supposed to be laid out? I ask as I followed on how to dissect the Discord webhook via its ID and TOKEN.

EDIT: I was able to get it to work "somewhat" by changing the script properties to the following:

WEBHOOKS || discord.com/api/webhooks/100000009...
TOKEN || hg_SMWJNfoo

Now when I click execute, it appears to fully execute. However, I am not receiving any messages in that Discord channel itself. Any ideas? The triggers are setup correctly. Unless it does not work when you manually execute it and just have to give it some time? Is there anything else I need to do once it's all setup? Here is what I have for the search criteria:

from: emailaddress@gmail.com

Also, see below for the script execution output:

Image description

Thread Thread
 
0xkoji profile image
0xkoji

That sounds your discord access right issue.
Are you sure that you allowed the bot/account to post a message to the channel?

Collapse
 
florisjan78 profile image
florisjan78

I am getting 401 unauthorized errors. You only see them when you set

'muteHttpExceptions': false

My variables are set as folows:
const webhooks = 'discord.com/api/webhooks/00002345140112979';
const token = 'nottherealtokenki56MxTtZW2opjtoWxuSICbOnuO';

which are the left and right part of the url from copy token in discord.

Is there a way to test if the webhook actually works?

another thing: with messages longer than 2000 I got a value assigned to const error, so I removed const from postMsg

Collapse
 
far2bribge profile image
Far2Bribge • Edited

If on the off chance you still need this, webhooks needs to include the https:// and include the token at the end: discord.com/api/webhooks/yourid/yourtoken.

To test the script, you need to run SendMailsToDiscord() and if any emails were recieved within the last 30mins (or your specified interval) it should send a discord message.

I was getting 405s for hours, turns out you need the token on the end of the url as well as a variable. If you run PostDiscord(postMsg) you should get a 400 error as the content is undefined.

Hope this helps if you still need it