#!/usr/bin/php
<?php
    
/* to make things simpler below */
    
function et($s){ return escapeshellcmd(trim($s)); }

    
/* Get the mailto contents */
    
$arg str_replace("mailto:"""$_SERVER["argv"][1]);
    
$arg explode("?"$arg);

    
/* Parse all the extra url arguments */
    
if(isset($arg[1]))
    {
        
parse_str($arg[1], $extra);
    }

    
/* set the url values to be used if the value is set */
    
$subject = (isset($extra["subject"]) ? "&su=".et($extra["subject"]) : "");
    
$cc = (isset($extra["cc"]) ? "&cc=".et($extra["cc"]) : "");
    
$bcc = (isset($extra["bcc"]) ? "&bcc=".et($extra["bcc"]) : "");
    
$body = (isset($extra["body"]) ? "&body=".et(urlencode($extra["body"])) : "");

    
/* construct the correct gmail url */
    
$gmail_url "https://mail.google.com/mail/?view=cm&tf=0&to=".et($arg[0]).$subject.$cc.$bcc.$body."&fs=1";

    
/* Runs firefox and opens a new window with the correct url */
    
exec("firefox -remote 'openurl(".$gmail_url.",new-tab)'");