Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

File I/O question

by sierpinski (Chaplain)
on Jul 07, 2009 at 16:41 UTC ( [id://777938]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I am working on a script that mails the contents of a file, but for some reason the first line is always truncated. I tested this by adding a blank line at the beginning of the file, then it seemed to work as expected. I'd like to see if someone can tell me where I've gone wrong here:
open(MAIL, "|mail $EMAIL"); print MAIL "To: $EMAIL\n"; print MAIL "From: $FROM\n"; print MAIL "Subject: $MAILSUB\n"; print MAIL "Content-Type: text/plain; charset=\"iso-8859-1\""; open(MESSAGE, "<", "$TMPFILE") or die "$!"; print MAIL <MESSAGE>; close MESSAGE; close MAIL;

Thanks!

Replies are listed 'Best First'.
Re: File I/O question
by afoken (Chancellor) on Jul 07, 2009 at 16:48 UTC

    You need an empty line between the last header and the content. And of course, you need a line end after the content type header. BTW: You should not use \n for network linefeeds, but \015\012. The former depends on the O/S, the latter doesn't, it always generates the required byte sequence. (\n works most of the time, because most servers are quite tolerant.)

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Discounting an obsolete build of Perl on an obsolete version of Mac, there's no OS dependency. The only difference is whether the :crlf I/O layer is used or not.

      The :crlf layer is used by default on Windows and in environments where it appears in $ENV{PERLIO}. Overriding the default PerlIO layers can avoid it, and the one-argument call to binmode disables it.

      With :crlf IO layer absent or disabled
      MacPerl*Everything else
      \nCRLF
      \rLFCR
      \012LFLF
      \015CRCR
      \015\012CR LFCR LF
      With :crlf IO layer present and enabled
      MacPerl*Everything else
      \nN/ACR LF
      \rN/ACR
      \012N/ACR LF
      \015N/ACR
      \015\012N/ACR CR LF

      MacPerl is an obsolete build of Perl for obsolete Macs. It predates PerlIO.

      Alexander,

      Thanks a lot, that did the trick. And thanks for the tip about the linefeeds. We are an all-Solaris shop for now, but you never know what upper management will decide when it comes to messing with our environment.

      Thanks!
        a better way to send mail is to use sendmail directly
        if you are on linux you can dosudo apt-get xmail and then use the following perl script
        use Mail::Mailer; $mailer=Mail::Mailer->new("sendmail"); $mailer->open ({ From=> <from address here>, To=> <to address here>, Subject=> <subject here>, }) or die "cant open mailer\n"; $body=<>; print $mailer $body; $mailer->close();
        the "sendmail" is to be replaced with the mail program like xmail (i use xmail)
        --------------------------------------------------------
        i'am worst at what do best and for this gift i fell blessed...
        i found it hard it's hard to find well whatever
        NEVERMIND
Re: File I/O question
by cbraga (Pilgrim) on Jul 07, 2009 at 18:03 UTC
    Better than fixing your missing line would be to use one of the perl modules that'll format the email body for you and send it out to the SMTP server according to the RFC.

    ESC[78;89;13p ESC[110;121;13p

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://777938]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found