Fetch message parts into a flat array with PHP IMAP

The PHP IMAP functions imap_fetchstructure and imap_fetchbody are used to work out the structure of an email and get the message body and attachments, but they can be fiddly to use because the message parts can be nested. This post has a function which effectively flattens the message parts into a new array, indexed by the part number which can be directly passed to imap_fetchbody.

Categories PHP

How to use static variables in a PHP function

There may be times when a static variable is needed in a PHP function; static variables maintain their value between function calls and are tidier than using a global variable because they cannot be modified outside of the function.

Categories PHP

Connect to SQL Server through an SSH tunnel

I recently needed to connect to a Microsoft SQL Server which I couldn’t reach directly, but which is visible from a web facing Linux machine. I’ve covered how to create an SSH tunnel on Windows with PuTTY already and then it’s simply a matter of connecting through the tunnel on the localhost to the SQL Server at the other end.

Creating an SSH port tunnel with PuTTY

This post shows how to do set up a port tunnel using the PuTTY SSH client on Windows. I don’t normally use Windows myself but on occasion need to use the SQL Server Management Studio to connect to a server on an unreachable internal network, but can connect to a Linux server that sits within that network. By port forwarding with PuTTY, I can use Management Studio to connect to the normally unreachable SQL Server.