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

I have some older code that was written for me some time ago. It used to work fine, but when I decided to use it again it's not putting the text file in the body of the message, it's sending it as an attachment. I'm able to run the mail command from command prompt and it works fine so I'm not sure what could be wrong with this code. Thanks for any help with this. This is the html form that sends the message.
<HTML><HEAD><TITLE>Email</TITLE> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1"> <META content="MSHTML 5.50.4207.2601" name=GENERATOR></HEAD> <BODY text=#000000 bgColor=#ffffff> <FORM name=form1 action=/cgi-bin/smail.pl method=post> <TABLE width="86%" border=0> <TBODY> <TR> <TD bgColor=#66ccff><B>User Notices:</B></TD></TR> <TR> <TD bgColor=#cccccc><B>Message:</B><BR><TEXTAREA name=txtMessage rows= +15 cols=75> </TEXTAREA> </TD></TR></TBODY></TABLE> <TABLE width="86%" border=0> <TBODY> <TR> <TD bgColor=#cccccc><INPUT type=submit value="Send Emails" name=Subm +it> </TD></TR></TBODY></TABLE></FORM> <P>&nbsp;</P></BODY></HTML>
This is the perl code
#!/usr/bin/perl -U ############################################################### # # 2001 - smail.pl # Send message to all users of system # ############################################################### #to add users to which you do not wish to send emails, just add email +to the dat file (one username per line) $NoSendFile = "donotsend.dat"; $MessageFile = "noticemsg.txt"; $AddrBookFile = "noticemail.dat"; $mailprog = "mail"; # Get information from office ################################### read(STDIN,$in,$ENV{'CONTENT_LENGTH'}); $in =~ s/\+/ /g; $in =~ s/%([a-fA-F0-9][a-dA-F0-9])/pack("C",hex($1))/eg; # Create a new record ######################################### $ParName = "txtMessage"; $ParTrailer = "&Submit=Send Emails"; if (index($in, $ParName) >= 0) { $MessageText = substr($in, index($in, $ParName) + length($ParName) + + 1); $MessageText = substr($MessageText, 0, rindex($MessageText, $ParTr +ailer)); } # Append Message to file ####################################### open(MESSAGE, ">$MessageFile") || die OnError($MessageFile); print MESSAGE $MessageText; print MESSAGE "\n"; close(MESSAGE); # Send message to each address book entry ###################### open RECIPIENT, $AddrBookFile || die OnError($AddrBookFile); open (FH, "$NoSendFile") || die OnError($NoSendFile); @nosendusers = <FH>; close(FH); foreach $nosenduser(@nosendusers) { chop ($nosenduser); } SendPageHeader(); # Open file of users not to send to ############################ while (<RECIPIENT>) { $sendto = $_; $sendto =~ s/\n//g; $sendto =~ s/ //g; $send = 0; foreach $nosenduser(@nosendusers) { if ($sendto eq $nosenduser) { $send = 1; } } if ($send eq 0) { ($sec,$min,$hour,$mday,$mon,$year,$wday,$isdst)= localtime(time); if (length($min) == 1) { $min = "0" . $min } if (length($sec) == 1) { $sec = "0" . $sec } $year += 1900; $mon += 1; $time = $mon . "/" . $mday . "/" . $year . " at " . $hour . ":" +; $time .= $min . ":" . $sec; $subject = "New Announcement"; $commandLine = $mailprog . " -s \"" . $subject . "\" " . $sendto; $commandLine .= " < " . $MessageFile; system ($commandLine); AppendUserToPage($sendto, $time); sleep 5; } } SendPageTrailer(); close RECIPIENT; # SemdPageHeader Method ####################################### sub SendPageHeader { # print "HTTP/1.0 200 OK\n"; print "Content-Type: text/html\n\n"; print "<html><title>Send Message Report</title><body>"; print "The message: <BR><BR>"; $MessageText =~ s/\n/<BR>/g; print $MessageText; print "<BR><BR> was sent to:<BR><BR>"; } # SendPageTrailer Method ###################################### sub SendPageTrailer { print "</body></html>\n"; } # AppendUserToPage Method ##################################### sub AppendUserToPage # AppendUserToPage(username, time) { my $username = shift(@_); my $currtime = shift(@_); print "<B> " . $username . " on " . $currtime . "</B><BR>"; } # OnError Method ############################################## sub OnError # OnError(filename) { my $FileName = shift(@_); # print "HTTP/1.0 200 OK\n"; print "Content-Type: text/html\n\n"; print "<html><title>Send Message Report</title><body>"; print "<P> This script has been unable to open the following file: + "; print $FileName . " </P>"; print "<P> Please make sure the file is not locked by another reso +urce "; print "and try again.</P>"; print " </BODY> </HTML>"; }

Replies are listed 'Best First'.
Re: Text file not in body of messag
by Corion (Patriarch) on Oct 29, 2014 at 08:11 UTC

    If you want to send an HTML body in your email, I think you need to send the appropriate MIME headers. If your mail client ("MUA") displays the HTML as attachment instead of inline, then maybe your mail client has changed instead of your program?

    Personally, I would use MIME::Lite for sending mail instead of manually creating a command line and then call system().