sgprog1 has asked for the wisdom of the Perl Monks concerning the following question:

Writing a simple program that basically emails some text to someone ($emailid):
open MAIL,"|/usr/bin/mailx -s '$subject' -r 'email\@email.com' $email +id"; print MAIL "Lines of Text go here"; close MAIL;
It works but the 1st line of the email is always the header "Content-length: ###" Any idea how to not have that "Content-length:" line display in the email. Thanks.

Replies are listed 'Best First'.
Re: content-length first line
by moritz (Cardinal) on Sep 15, 2009 at 20:03 UTC
    I recommend using a proper module for sending emails, MIME::Lite is often recommended, and I so far my experience with it has been very good.

    Your approach is rather fragile and might be a source of trouble, for example if $subject or $emailid are not properly escaped.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: content-length first line
by Corion (Patriarch) on Sep 15, 2009 at 20:05 UTC

    Preferrably over Sewi's solution, I'd look at using MIME::Lite instead of opening a socket to whatever incarnation of sendmail lives on your system directly. It's far less spam-prone and works even on Windows.

Re: content-length first line
by Sewi (Friar) on Sep 15, 2009 at 19:54 UTC
    Try
    print "[$subject] [$emailid]\n";
    I guess you got a newline char at the end of one of them which will show up in a linebreak before the closing bracket.
    If this doesn't help, try
    print "open MAIL,\"|/usr/bin/mailx -s '$subject' -r 'email\@email.com' + $email +id\";\n"; print "Lines of Text go here";
    to see what you're really passing to mailx. Checking your inbox on the mailserver (usually in /var/spool/mail/) may also help.
      If you're right (and you probably are), it's odd and unfortunate that the mailer blindly inserts the newline rather than escaping it or throwing an error. boooo!
Re: content-length first line
by Sewi (Friar) on Sep 15, 2009 at 20:56 UTC
    I agree with you two, a Module is a better solution for sending mail, but I tried to solve the actual problem and if there are any newlines, the Module could do the same suspicious things but is harder to debug.
    Personally, I prefer piped mail program when writing one-time scripts which need to mail something usually to one person (usually me). But these scripts never leave the machine where they're written and I know the mail server installation.