There should be no real difference between running from the command-line and from a Perl script. If you run your script from the command-line, does the message take a long time to send? What about if you try a second time? What if you switch to the Web server user before doing so? Do you do anything "weird" in your Web server setup, like
? What shows up in your Web server log? In your mail server log? Do you see the exact same effect if you run the small test script you included in your OP, both running from the command-line and the Web server?