Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Email format issue

by mfali (Initiate)
on Apr 18, 2011 at 07:36 UTC ( [id://899889]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Everyone,

I am a newbie to perl and have been facing issue when sending email via my perl script. The script I have generates a list of patches failing to meet some specific criteria. The list of patches is printed and placed at the output directory of the apache server.

My job requires me to send this list as an email to my co-workers. I am using the code below for this purpose

sub email_report{ my $subject='Excluded checkins after lockdown mode' ; my $mail=''; open MAIL,">/home/mfali/Workscripts/tmpfile.html" || die $!; print MAIL "\n"; print MAIL "To: $mail\n"; print MAIL "Subject: $subject\n"; print MAIL "Mime-Version: 1.0\n"; print MAIL "Content-Type:text/html\n"; open REPORT, "/home/mfali/public_html/my_report1/out.html" || die $!; while (<REPORT>) { print MAIL $_ ; } close REPORT; close MAIL; # Send the email system("/usr/lib/sendmail -t -oi < /home/mfali/Workscripts/tmpfile.h +tml"); }

When I look at the format of the patches either in the apache output directory or if I look the tmpfile.html , then the format looks correct. But when the same list comes through email, it's format is getting corrupted. I have been trying to resolve this issue for the past 2-3 days and have not been successful. I am not allowed to install any modules and either have to use the core modules or use sendmail. Can some one please suggest me as to how can i effectively send a report in the email keeping its format intact.

Using thunderbird mail client, with perl, v5.6.1 built for i686-linux.


Replies are listed 'Best First'.
Re: Email format issue
by wind (Priest) on Apr 18, 2011 at 07:44 UTC

    You need two returns after the content-type

    print MAIL "Content-Type:text/html\n\n";

    Also note that you'll want to escape the @ in your From email.

    print MAIL "From:zzz\\n";
      Hi Wind,

      Thank you for the suggestion. Adding a \n to as per your suggestion did the trick and I am now getting the expected results. Thank you so much everyone.

Re: Email format issue
by Corion (Patriarch) on Apr 18, 2011 at 07:42 UTC

    My guess is that the problem is that you don't have an empty line between your last mail header and the first line of your mail text. But I really do recommend MIME::Lite for sending email. It takes care of most of the things like headers and attachments etc.

Re: Email format issue
by jwkrahn (Abbot) on Apr 18, 2011 at 11:11 UTC
    open MAIL,">/home/mfali/Workscripts/tmpfile.html" || die $!; ... open REPORT, "/home/mfali/public_html/my_report1/out.html" || die $!;

    Your open statements will not die because the || (logical OR) operator has higher precedence than the , (comma) operator.

    You need to either use parentheses for open:

    open( MAIL,">/home/mfali/Workscripts/tmpfile.html" ) || die $!; ... open( REPORT, "/home/mfali/public_html/my_report1/out.html" ) || die $ +!;

    Or use the lower precedence or operator:

    open MAIL,">/home/mfali/Workscripts/tmpfile.html" or die $!; ... open REPORT, "/home/mfali/public_html/my_report1/out.html" or die $!;
Re: Email format issue
by Xilman (Hermit) on Apr 18, 2011 at 07:41 UTC

    In what way is the mail being corrupted?

    I don't see anything obviously wrong with your script but that may just be short-sightedness on my part. If we had more information we might be able to help out more effectively.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://899889]
Approved by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-14 13:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found