Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

MIME Lite no line feed in Message body

by sudheer157 (Initiate)
on Jun 26, 2011 at 11:50 UTC ( #911446=perlquestion: print w/ replies, xml ) Need Help??
sudheer157 has asked for the wisdom of the Perl Monks concerning the following question:

Hi i wrote a simple script to send email using MIME:Lite on Linux machine. I am trying to write the email body from a flat file, but the new line characters are not appearing in the email body. Sample code:
my $mime_msg = MIME::Lite->new( From => $from_user, To => $to_users, Bcc => $bcc_users, Cc => $cc_users, Subject => $subject, Type => "multipart/related") or die "Error creating MIME bod +y: $!\n"; my $body='/tmp/file.txt' $mime_msg->attach(Type => 'text/html', Path => $body, Filename => $bod +y ) or die "Error attaching html: $!\n"; $smtp->data(); $smtp->datasend($message_body); $smtp->dataend(); #close connection $smtp->quit();

=== file1.txt ==

Line one
line two
line three

I am able to send an email but the body of the email is not as expected. the new lines are stripped out.

===== Mail body ===

Line one line two line three

I am wondering hot to get the mail body as is in the file1.txt ie line breaks are missing. I am not sure why perl is not considering the new line chars as line breaks. Appreciate you help.

Comment on MIME Lite no line feed in Message body
Download Code
Re: MIME Lite no line feed in Message body
by Corion (Pope) on Jun 26, 2011 at 11:54 UTC

    Your content type is text/html. HTML line breaks are different from text line feeds.

    Either learn about HTML or use text/plain as your content type. You might also want to think about the character encoding and specify that as well.

      Thanks Corion,
      changing it to "text/plain" resolved the issue.

      is there any way perl can interpret the format automatically if the file is a plain or html, because my case is the body file may be a plain or html file. so really need a way to identify the format and use plain or html accordingly. Any Ideas?
        "so really need a way to identify the format and use plain or html accordingly."

        If you're sending it, you should know what the content is. So, no, you don't "really need" some external evaluation of the file type... unless you're being paid to send spam without reviewing the content :-)

        That said, you might also consider a file test construct like:

        unless -T $FH ...(do something) See http://perldoc.perl.org/perlfunc.html

        But - - IMO - - it's "the long way around Robin Hood's barn" to code up file-type-test procedures and translate their output to something you can stick into $mime_msg->attach(Type => '.... unless you have some need to send files whose type is not readily discernable by inspection... or such a volume to send that inspection is impractical.

        PS, just in case: the idiom marked with quotes in the previous para translates (roughly) to "the hard way" or "a poor way."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://911446]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2014-12-18 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (48 votes), past polls