Any ideas why some of the variables are appearing differently?
No. Perhaps it's something that's happinging after the fact. Try printing out $text before you construct the mail and seeing if it layed out correctly.
I'm not sure what this line is doing:
sprintf('%-'.$nd.'s %-'.$vd.'s',
Rathar than ask us to explain everything you could possibly being having trouble understanding, it would more efficient if you could be more precise about what about this you are finding difficult to understand.
- Something documentation for the sprintf()
- The effect of '-' in printf templates?
- Concatenation of strings using the . operator?
- Something else...?