Gmail to RSS ezpz guide
step 1: prep work
- go to script.google.com and click New Project
- delete everything inside the editor and paste the script below
- replace
secret_key with a password of your liking. you will use this in your feed URL.
the script:
var SECRET_TOKEN = "secret_key";
var searchQuery = 'in:inbox';
var maxEmails = 15;
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 + '<br/><br/>' + 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 '<';
case '>': return '>';
case '&': return '&';
case '\'': return ''';
case '"': return '"';
}
});
}
step 2: deploy
- click the Save icon at the top.
- in the top right corner, click the blue Deploy button and select New deployment.
- click the Gear icon next to "Select type" and choose Web app.
- fill out the configuration, description can be customized.
- Description: gmail to RSS
- Execute as:
Me (your email address) - Who has access:
Anyone
- choose Deploy.
- 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.