my $MailText = ''; open MAILOUT, '>', \$MailText or die "open in-memory output: $!"; select MAILOUT; # ... print "foo\n"; # ... print "bar\n"; # ... close MAILOUT; select STDOUT; # $MailText now contains "foo\nbar\n"; send it however you want