If Perl doesn't die or at least freeze, then perl is not running out of memory. perl will let you know well ahead of time when you are out of memory, overflow/ underflow exceptions and all of that other fun stuff that you learned in UNIX programming 101. Try the same script using a test case with output to the screen. perhaps a bug somewhere is hitting an infinite loop? In any case, you will want to view your message and compare that with what you expected.
is certainly a worthwhile option. It is also quite possible that this error is serverside. If you encounter further problems that you decide you can't get around, an alternative would be to pipe the message straight to sendmail. But you sure be sure that what you have now can't be fixed before doing that.