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

Dear Monks,

I am facing issues while sending mail using Mail::Mailer module.I need a way to send body containing multiple lines using the Mail::Mailer module. Hope you people might have faced this issue at any of your tasks which used Mail::Mailer. The requirement is, I have to read the contents from a file and need to send the file contents as the body message to the mail.

The problem here is, when i send the mail, the body contains only the first line of the file. The remaining contents are not available in the body part. But, if i replace the newline characters from the file contents with the string  '<br>', the remaining contents are coming properly.

I need your valuable suggestions for,


Here is my code,
my $mailer = new Mail::Mailer 'smtp', Server => 'localhost' or die "Co +uldn't create a mail object!\n"; my $mail_headers = { 'Content-Type' => 'text/plain', #I tried with 'text/html' for html + body. But that one was also failed :( To => [ $to ], From => 'root@mydomain.com', Subject => $subject, }; $mailer->open( $mail_headers ); $mailer->print($mailContents); #$mailContents - Contains the file cont +ents with new lines. $mailer->close();
Thanks for your valuable time.

Regards,
S.Venni Rajan.
"A Flair For Excellence."
                 BK Systems.

Replies are listed 'Best First'.
Re: How to send mail containing multiple lines using Mail::Mailer?
by whereiskurt (Friar) on Mar 14, 2007 at 13:28 UTC

    The code attached works for me and if it works for you then you've got other issues (mail server, environment, bad luck) ;-) One quick comment about the code snippet you posted - you're mixing camelCase ($mailContents) with underscore_case ($mail_headers). My advice is to pick one, and make sure you 'use warnings' at the top of you code.

    One thing to _maybe_ try is using '\r\n' or just '\r'. Maybe your mailserver is wonky. :)

    KPH
    use strict; use warnings; use Mail::Mailer; my $mailer = new Mail::Mailer 'smtp', Server => 'localhost' or die "Co +uldn't create a mail object!\n"; my $mail_content = "Here is a first line.\nHere is a second line.\n"; my $mail_header = { 'Content-Type' => 'text/plain', To => [ 'xxxxxxxxxxxxxxxxxxxxxxx' ], From => 'xxxxxxxxxxxxxxxxxxxxxxx', Subject => 'Just some subject.', }; $mailer->open( $mail_header ); $mailer->print($mail_content); $mailer->close();
Re: How to send mail containing multiple lines using Mail::Mailer?
by hangon (Deacon) on Mar 14, 2007 at 10:58 UTC

    Just a guess, but does your content have a line with only a dot followed by a newline? This indicates end of message.

    .\n
      No, It don't have anything like that. The file contains only alphabet data.
Re: How to send mail containing multiple lines using Mail::Mailer?
by shmem (Chancellor) on Mar 14, 2007 at 11:05 UTC
    From the manual page, is seems that
    print $mailer $mailContents;

    is the required syntax. Did you try that? It seems that the $mailer object is really a filehandle.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Yes, I have tried this way also. But unfortunately, that one also failed :(
Re: How to send mail containing multiple lines using Mail::Mailer?
by chargrill (Parson) on Mar 14, 2007 at 17:37 UTC

    Where's the code that fills $mailContents?

    Does it perhaps look like this:

    my $file = 'mailmessage'; open( my $fh, $file ) or die "couldn't read file - $!"; my $mailContents = <$fh>; close( $fh );

    ... or similar?

    You might instead want:

    my $file = 'mailmessage'; open( my $fh, $file ) or die "couldn't read file - $!"; my $mailContents; { local $/; $mailContents = <$fh>; } close( $fh );

    HTH



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)