Kindle – Unofficial Guardian Subscription

Having received a new 4th gen Kindle for christmas I signed up for the Guardian paper 2 week trial from Amazon. I’m quite impressed at how good the newspaper reading experience is like on the Kindle. The paper gets delivered everyday at around 6am, so when I get on the train, todays issue is sat at the top of my home page ready to be read. The subscription currently costs £10 per month, which isnt too bad, they charge £1 per issue if you don’t want to subscribe.

Guardian Kindle

I came across this blog post from Mark Longair, he has used the Guardian’s own open api to create a Kindle version of the paper, in fact he did this before the Guardian had their official Kindle version to download. Mark has the most recent edition on a page on his site, this is created ‘early’ each day. Now having compared the paid and this ‘free’ version, there isnt much difference, the majority of articles are there, including the magazines etc. I wanted a way to get this delivered to my Kindle every morning, much the same as my current paid subscription works.

Amazon allow you to email your kindle with attachments, which are then automatically downloaded to your device. So I set about creating a simple PHP script that will allow me to download the latest issue and email it to my kindle. Below is the code I used, I couldn’t get the attachment to work if the file was on a remote server, so the first thing the script does, is go and grab the todays issue from blah blahs website. Then the newly grabbed file is then attached to an email and sent to the specified email address.

Simply replace the To, From, Subject and Message variables with which ever values you require. Make sure to add the from address your going to use to your Amazon/Kindle account, to authorise this email address for delivery to your device.

<?php
 
 $filename = "gdn-".date("Y")."-".date("m")."-".date("d").".mobi"; 
  
 $handle = fopen("http://mythic-beasts.com/~mark/random/guardian-for-kindle/".$filename, "rb");
if($handle){
  $somecontent = stream_get_contents($handle);
  fclose($handle);

  $handle = fopen($filename, 'wb');
  if($handle){
    if (fwrite($handle, $somecontent) === FALSE) {       
       exit;
    }    
    fclose($handle);
  }else{     
     exit;
  }
}
 
// array with filenames to be sent as attachment
$files = array($filename);
 
// email fields: to, from, subject, and so on
$to = "hello@email.com";
$from = "me@me.com"; 
$subject ="subject"; 
$message = "message";
$headers = "From: $from";
 
// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
 
// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";
 
// preparing attachments
for($x=0;$x<count($files);$x++){
	$file = fopen($files[$x],"rb");
	$data = fread($file,filesize($files[$x]));
	fclose($file);
	$data = chunk_split(base64_encode($data));
	$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
	"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
	"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
	$message .= "--{$mime_boundary}\n";
}
 
// send
 
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
	echo "<p>mail sent to $to!</p>"; 
} else { 
	echo "<p>mail could not be sent!</p>"; 
} 
 
?>

If there is any interest I may provide a service where you provide me with your kindle email address and I’ll add you to the list to receive the unofficial version as well :).

Sources: PHP email with attachments

Leave a Reply

Your email address will not be published. Required fields are marked *

*