Create a Gmail RSS Feed with PHP

This cool PHP function will create an RSS feed from the email in your Gmail inbox. It’s a good way of including your mail with all the other content you aggregate in your RSS reader.

The PHP Function

Rather than copy the following code, you should download the code below.

<?php
function checkGmail($username, $password)
{ 
 $url = "https://mail.google.com/mail/feed/atom"; 

 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, $url);
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
 curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 curl_setopt($curl, CURLOPT_ENCODING, "");
 $mailData = curl_exec($curl);
 curl_close($curl);
 
 return $mailData;
}

header('Content-Type:text/xml; charset=UTF-8');
$feed = checkGmail("YourUsername@gmail.com", "Password");
echo $feed;
?>

The Result

The result will look like any other RSS feed (in the example image below I’ve had to blur out personal details… but I’m sure you’ll get the idea).

Gmail RSS Feed

Gmail RSS Feed

Including Gmail RSS Feed in Your WordPress Page or Blog

If, for some reason, you chose to include an RSS feed in your WP page or blog, you can use shortcode I’ve previously provided.

[rss feed="http://www.domain.com/Gmail-RSS.php"]

Errors

The most likely error you’ll encounter is something similar to:

curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in ....

No need to worry… CURL’s CURLOPT_FOLLOWLOCATION really isn’t required for the function to do its thing – so you can simply comment out line 8.

Security

You should consider some security if you choose on using this function for your own personal email. The least secure (and easiest) means of security might be creating a complicated filename… or adding an ID in the URL that only you are aware of (and comparing it against the same ID in the code).

Another very dodgy means of security might be passing your username and password in the URL (http://www.domain.com/Gmail-RSS.php?username=something&password=somethingelse) and using $_GET in the code. This method, however, isn’t at all secure and can easily be intercepted as web traffic.

An RSS Feed without the Code

After my rant on security, here’s a very insecure way of generating a feed. If nothing else, It’ll give you an idea of the end result. Simply copy the following into your URL and replace your username and password. No details are recorded and no cache is created on our end.

http://gmail-rss.internoetics.com/gmail.php?username=YOURUSERNAME@gmail.com&password=YOURPASSWORD

It’s likely I’ll build a more robust and secure tool in the future.

First Name:
Your Email Address:
 




Download: Create an RSS feed from email in your Gmail email account
Description: Create an RSS feed from email in your Gmail email account (with PHP and CURL).
Author:Marty
Category: PHP code
Date: August 5, 2012



If you liked this article, you may also like:

  1. [Shortcode] Include an RSS feed in a post or page with shortcode
  2. Exclude posts from WordPress blog or RSS feed based on a tag
  3. Add author details after each post in a WP RSS feed
  4. Twitter using CURL via their API
  5. Add Your Latest Google Plus Post(s) to Your WordPress Blog (or Generate a Google+ RSS Feed)
About Marty

is a passionate web developer from Sydney, Australia. He owns about 600 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. Follow Marty on Twitter or Google+.

Comments

  1. Glenn says:

    Marty – Thanks for this post. Working on doing some work where anyone can send to an email address and have it show up in a twitter stream. I’m able to get the one that runs through your website (http://gmail-rss.internoetics.com/gmail.php …) working but I can’t get it working from my website. I’ve uploaded the PHP script to my my website but when I put my credentials in the URL it returns a page showing “Unauthorized” for title and H1 under Body. Any thoughts on what I’m doing wrong? Thanks!

    • Marty says:

      Hi Glenn. I really don’t know. Perhaps it has something to do with either the cURL request (is cURL enabled on your server) or the http request (try http:// instead of https://). I just don’t know!

      I did something similar to you some time back for an email shoutbox that tweeted all incoming message subjects to Twitter. I’ll see if I can find the old code and send it to you. I saved all email to a database first; so I ran two cronjobs: one to retrieve the email and another to run the Twitter part.

Speak Your Mind

*