Releasing xkcd-mailer to the world

This commit is contained in:
Ismo Vuorinen
2010-08-23 20:19:40 +03:00
commit 124a12dfe0
2 changed files with 51 additions and 0 deletions

10
README Normal file
View File

@@ -0,0 +1,10 @@
# xkcd-Mailer #
Takes the first/latest item from the xkcd atom-feed and mails the image and punchline to a specified email address.
## crontab example ##
5 minutes over 7am on monday, wednesday and friday.
5 7 * * 1,3,5 /usr/bin/php /home/users/viir/code/php/xkcd-mailer.php

41
xkcd-mailer.php Normal file
View File

@@ -0,0 +1,41 @@
<?php
/**
* xkcd-Mailer
*
* Sends HTML-email containing hotlinked latest xkcd
* strip with alt/title-text underneath the image.
*
* @author Ismo Vuorinen
* @version $Id$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @copyright 23 August, 2010
* @package default
**/
// Your timezone, PHP5 required.
date_default_timezone_set("Europe/Helsinki");
// Your destination
$mail = "ivuorinen@gmail.com";
$from = "xkcd mailer <xkcdmailer@example.com>";
$feed = "http://xkcd.com/atom.xml";
$data = simplexml_load_file($feed);
$item = $data->entry[0];
$date = date("Y-m-d", strtotime($item->updated));
preg_match("#title=\"(.+)\"#iU", $item->summary, $t);
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= 'From: '. $from . "\r\n";
$subject = "xkcd {$date}: {$item->title}";
$punchline = $t[1];
$msg = "<h1><a href=\"{$item->id}\">{$item->title}</a></h1>\n"
. "<small>Posted {$date}</small><br />\n"
. $item->summary."<br />\n"
. "<p>{$punchline}</p>\n";
mail($mail, $subject, $msg, $headers);