Adding Attachments To Email Using the MailGun.Com PHP API

When sending messages via mailgun I really struggled to add an attachment using the obvious method:

$mg = new Mailgun('mailgun-api-key');
$msg = $mg->MessageBuilder();
$mg->post("{$domain}/messages", $msg->getMessage());

The solution was to do this….

$mg = new Mailgun('mailgun-api-key');
$msg = $mg->MessageBuilder();
$files['attachment'] = array();
$files['attachment'][] = '/var/www/somefile.php';
$mg->post("{$domain}/messages", $msg->getMessage(), $files);

Adding the HTTP Extension to PHP on Ubuntu 12.04 LTS

Quite often I forget the requirements of the php_http / pecl_http extension which is required for some network actions. Here is how to install most of what’s required.

apt-get install libcurl3 php5-dev libcurl4-gnutls-dev libmagic-dev
pecl install pecl_http

Then edit the two php.ini files

nano -w /etc/php5/apache2/php.ini
nano -w /etc/php5/cli/php.ini

to include the line

Then restart Apache

/etc/init.d/apache2 restart

Installing GearmanD on Ubuntu 12.04 LTS (Precise Pangolin) With PHP Support

I’m sure you’ve already visited many, many sites which explain how to install GearmanD on Ubuntu – but you’ve continued searching because something broke or didn’t quite work out. I too was in the same situation and spent 3 days battling through bits and pieces of information scattered across the web.

Here is the process I went through to install Gearman 1.1.7 on a Rackspace/OpenStack Ubuntu 12.04 instance which (assuming decent connection etc) should take less than 30 minutes!

Make a server instance

Obviously skip this part if you are working in Rackspace cloud or have already provisioned an Ubuntu instance.

  1. Log into your control panel at for UK Rackspace customers or for US customers
  2. Click “Hosting”
  3. Click “Cloud Servers”
  4. Scroll down and click “Select” next to Ubuntu 12.04 LTS (Precise Pangolin)
  5. Give your server a name
  6. Select your server size – the size you choose will be dependent on the workload/throughput you expect your server to do, if you need lots of things done quickly with no regard to expense choose a larger server, however if you just want this gearmand server to plod through jobs then a smaller server will probably do.
  7. Click “Launch”
  8. Make a note of the password which is shown during set up – you will need it to log in to the server.

Just sometimes…

All you need to do is:

apt-get install gearmand-server

Install Ubuntu Update & Gearman Prerequisites/Dependancies

  1. Log into your server using your SSH client (I use puTTY)
  2. If you’ve just created the instance, change your password by entering:
  3. Update & Upgrade your Ubuntu to have the latest patches etc by running:
    apt-get update
    apt-get upgrade
  4. Because my Ubuntu instance was a clean install I needed to install the LAMP stack – there will be dependencies in here which Gearman requires – such as MySQL – if you’re not installing the LAMP stack you will need to do some futher reading! I use tasksel to do my LAMP install (because I’m lazy)
  5. Install Gearman’s dependencies:
    apt-get install build-essential libboost-thread-dev libboost-thread1.46-dev libcloog-ppl0 libboost-program-options-dev libevent-dev php5-dev libcloog-ppl0

Installing GearmanD

  1. Figure out which version of Gearman you need by visiting – in my case I’m using 1.1.7 so in the subsequent instructions change 1.1.7 to your version number
  2. Get the version of Gearman you require and unpack it:
    cd /usr/src
    tar -xvzf gearmand-1.1.7.tar.gz
    cd gearmand-1.1.7
  3. Make & Install GearmanD
    make && make install
    cd ..

All being well you should now have Gearman install and nothing broke/exploded or otherwise shit its pants during the install process.

Installing the PECL Extension for Gearman

To use Gearman with PHP we need to install the PECL extension – to do that we:

  1. Figure out which version we need by visiting – in my case I’m using 1.1.1 so in the subsequent instructions change 1.1.1 to your version number
  2. Download & unpack the appropriate file
    tar -xvzf gearman-1.1.1.tgz
  3. Install the PECL extension:
    cd gearman-1.1.1
    make install
    cd ..

Update PHP with Gearman Extension

You made it to the last step! Getting PHP to recognise Gearman…

  1. You need to update your php.ini file(s) with the Gearman extension. As I intend to use Gearman both from the command line and Apache I need to update both of my php.ini files. To locate them I used:
    locate php.ini
  2. Which told me I need to edit /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini. I’m not a huge fan of vi so I use nano to edit instead:
    nano -w /etc/php5/apache2/php.ini
    nano -w /etc/php5/cli/php.ini

    I added the following to each php.ini file:

In Conclusion

You should now have a working Gearman server – hopefully it took you less than 30 minutes and can now go make a nice cup of tea!