The only thing that looks a little odd is that you set $p to a string variable, i.e.:
$p = $p->message;
... then try to access the string variable as if it were still an object, i.e.:
$tmpl->setVar('mail_message', $p->message);
Try removing the $p = $p->message; line and see if it makes a difference.