Gmail to RSS guide

Guide

t f B! P L


Gmail to RSS ezpz guide

step 1: prep work

  1. go to script.google.com and click New Project
  2. delete everything inside the editor and paste the script below
  3. replace secret_key with a password of your liking. you will use this in your feed URL.
    the script:
// enter your password here, do not share this with anyone.
var SECRET_TOKEN = "secret_key"; 

// filter, change it if you like
var searchQuery = 'in:inbox'; 
var maxEmails = 15; // control how much email will be fetched, change it as your liking

function doGet(e) {
  if (!e.parameter.token || e.parameter.token !== SECRET_TOKEN) {
    return ContentService.createTextOutput("Unauthorized as the auth token is invalid")
      .setMimeType(ContentService.MimeType.TEXT);
  }

  var threads = GmailApp.search(searchQuery, 0, maxEmails);
  var rss = '<?xml version="1.0" encoding="UTF-8"?>\n';
  rss += '<rss version="2.0">\n';
  rss += '  <channel>\n';
  rss += '    <title>My Gmail Feed</title>\n';
  rss += '    <link>https://mail.google.com</link>\n';
  rss += '    <description>Recent emails matching: ' + searchQuery + '</description>\n';

  for (var i = 0; i < threads.length; i++) {
    var message = threads[i].getMessages()[0]; 
    
    var subject = message.getSubject() || '(No Subject)';
    var sender = message.getFrom();
    var date = message.getDate().toUTCString();
    
    var body = message.getPlainBody(); 
    var link = 'https://mail.google.com/mail/u/0/#inbox/' + message.getId();
    subject = escapeXml(subject);
    sender = escapeXml(sender);
    body = escapeXml(body);

    rss += '    <item>\n';
    rss += '      <title>' + subject + '</title>\n';
    rss += '      <link>' + link + '</link>\n';
    rss += '      <description>From: ' + sender + '&lt;br/&gt;&lt;br/&gt;' + body + '</description>\n';
    rss += '      <pubDate>' + date + '</pubDate>\n';
    rss += '      <guid isPermaLink="false">' + message.getId() + '</guid>\n';
    rss += '    </item>\n';
  }

  rss += '  </channel>\n';
  rss += '</rss>';

  return ContentService.createTextOutput(rss)
    .setMimeType(ContentService.MimeType.XML);
}

function escapeXml(unsafe) {
  if (!unsafe) return '';
  return unsafe.replace(/[<>&'"]/g, function (c) {
    switch (c) {
      case '<': return '&lt;';
      case '>': return '&gt;';
      case '&': return '&amp;';
      case '\'': return '&apos;';
      case '"': return '&quot;';
    }
  });
}

step 2: deploy

  1. click the Save icon at the top.
  2. in the top right corner, click the blue Deploy button and select New deployment.
  3. click the Gear icon next to "Select type" and choose Web app.
  4. fill out the configuration, description can be customized.
  • Description: gmail to RSS
  • Execute as: Me (your email address)
  • Who has access: Anyone
  1. choose Deploy.
  2. google will ask you to authorize access, click Authorize access and select your account. if it says "Google hasn’t verified this app" then click Advanced -> Go to [Project Name] (unsafe) and allow the permissions.

step 3: here we go

after you deploy the script, google will give you a Web app URL that looks like this:
https://script.google.com/macros/s/abcdefgh.../exec

note: if you are confused about which link is correct, simply searching for this string specifically in the url: /macros/s/

to use this in your RSS reader, append your secret token to the end of the URL like this:
https://script.google.com/macros/s/abcdefgh.../exec?token=secret_key

Do not share the secret key or the webapp url with anyone.
And now you have it, have a nice day.

Artist who wraw the picture in the thumbnail: @cola_submarine on twitter.

Term convert

Terms convert

Enter terms you want to replace while reading

Search This Blog

Featured Post

Frequently Asked Questions

Q: What is the purpose of Nanjamo Atelier? A: to do what i desired of Q: i want to contact you. A:  Via Twitter: Calmery_ Q: Update frequenc...

About Me

Labels

Guide (1) meta (1) novelTL (3)

Blog Archive

QooQ