Random

So you wanted to know what Random was did you? Well it does exactly what it says on the tin. This page plays host to a random post on every page refresh.

It’s a plugin that you can get hold of here. Currently it displays a random published post but I might be extending it somewhat. Suggestions welcome.

The content below this line is generated by the SB Random Post plugin.
This post was published on 2013-05-18 16:58:04 by sean
You might have heard of Mailchimp… who hasn’t these days! Well the same guys have released a rather nice little system called Mandrill (or did earlier this year at least). It is a transactional SMTP relay service with a rather nice little API for those who care.

For those that don’t give a monkeys (get it.. monkeys?!) I have a barebones little bit of PHP code for you to use which will effect the sending of your emails. Most sites using PHP have a central ‘mail’ command. There will be a function in a file or include somewhere which will handle the sending of mail using this sort of syntax:

mail($to, $subject, $body);

Quite simple.. it’s how PHP sends emails normally. The following code is to replace that mail command. Don’t forget to enter your API key!

$to = 'test@test.com';
$content = '<p>this is the emails html <a href="www.google.co.uk">content</a></p>';
$subject = 'this is the subject';
$from = 'test@mandrill.com';

$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$api_key = 'YOUR KEY HERE';
$content_text = strip_tags($content);

$postString = '{
"key": "' . $api_key . '",
"message": {
 "html": "' . $content . '",
 "text": "' . $content_text . '",
 "subject": "' . $subject . '",
 "from_email": "' . $from . '",
 "from_name": "' . $from . '",
 "to": [
 {
 "email": "' . $to . '",
 "name": "' . $to . '"
 }
 ],
 "track_opens": true,
 "track_clicks": true,
 "auto_text": true,
 "url_strip_qs": true,
 "preserve_recipients": true
},
"async": false
}';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);

You can also echo the $result variable to debug this code.. it will return a string of JSON with either a success message or an error. Rather useful I think!

2 comments

  1. Alan says:

    Hi Sean, my website currently uses a code that displays a random post on every homepage refresh. I’m actually interested in seeing you create something that will show all the site posts in order than randomly.

    Alan

    • Sean says:

      Hi Alan,

      Thanks for the suggestion. I may just do that. It should be easy enough to grab a list of all of the posts in ID order focussed on the last one shown and then increment a DB counter with each page refresh. Certainly doable in a couple of hours’ effort.

      chers
      S

Leave a Reply