How to attach dynamic data with $message->attachData

I spent ages looking at how to do this, having it in the docs would be brilliant for others.