27db Attach text file to email - Perl Programming
Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Attach text file to email

Posted by hugo_owen (hugo_owen), 9 May 2005
Trying to attach a text log file with perl and I keep getting the following message when the email comes in my inbox - this error is n the message body:

Subject: MIME-Version: 1.0Content-Description: Replacement for objectionable contentContent-Type: Text/HTMLX-NAIMIME-Modified: 1<html><head><meta HTTP-EQUIV="Content-Type" content="text/html"><meta HTTP-EQUIV="charset" content="utf-8"> <title>CORRUPT MAIL ALERT</title></head>    <body> <h1><font color="#FF0000">CORRUPT MAIL ALERT</font></h1> <p>A message or attachment was found to be corrupt and has been removed.</p> <p>Cause of corruption: Invalid headers. </p> <p>Copyright &copy; 1993-2003, Networks Associates Technology, Inc.<br>      All Rights Reserved.<br> <a href="http://www.mcafeesecurity.com">http://www.mcafeesecurity.com</a></p> </body></html>





Below is the code: - Please help - driving me mad.

use Net::SMTP;
use MIME::Lite;

my $nodename = $ENV{"COMPUTERNAME"};
my $from_address =  "${nodename}\@arup.com";
my $to_address = 'email_address';
my $subject = 'MIME Net::SMTP test';
my $mime_type = 'TEXT';
my $message = "Hello world!\n";

# Create the initial text of the message
my $mime_msg = MIME::Lite->new(
  From => $from_address,
  To   => $to_address,
  Subject => $subject,
  Type => $mime_type,
  Data => $message
  )
 or die "Error creating MIME body: $!\n";

my $filename = 'c:\tmp\test.txt';

# Attach the test file
$mime_msg->attach(
#   Type => 'application/zip',
  Type => 'TEXT',
  Path => $filename,
  )
 or die "Error attaching test file: $!\n";

my $message_body = $mime_msg->body_as_string();
my $servername = "smtp@arup.com";
$smtp = Net::SMTP->new($servername);
       die " couldn't connect to server" unless $smtp;

$smtp->mail( $from_address );
$smtp->to( $to_address );

$smtp->data();
$smtp->datasend($message_body);
$smtp->dataend();


Posted by admin (Graham Ellis), 10 May 2005
Have you tried saving out the message_body to a file on the machine that's generating the email and seeing what's there - it might well give you the clue.   The error message that you've posted is from your own mail filtering system and it's removing the evidence of what the real problem is ...

Posted by hugo_owen (hugo_owen), 10 May 2005
Thanks Graham, I've printed out to a file but it does'nt mean much to me any ideas what wrong??

This is a multi-part message in MIME format.

--_----------=_111571609534560
Content-Disposition: inline
Content-Length: 13
Content-Transfer-Encoding: binary
Content-Type: text/plain

Hello world!

--_----------=_111571609534560
Content-Disposition: inline; filename="test.txt"
Content-Length: 14
Content-Transfer-Encoding: binary
Content-Type: text/plain; name="test.txt"

THIS IS A TEST
--_----------=_111571609534560--




Posted by admin (Graham Ellis), 10 May 2005
Looks like you need to initialise your MIME boundary.  I would expect to see something like this as part of the email header

Code:
MIME-Version: 1.0
Content-Type: multipart/alternative;
     boundary="09845039450394rahhdkY-cmwdwiirmar"


Then the sections look something like

Code:
--09845039450394rahhdkY-cmwdwiirmar
Content-Type: text/plain;
     charset = "ISO-8859-1"
Content-Transfer-Encoding: 8bit



--09845039450394rahhdkY-cmwdwiirmar
Content-Type: text/html;
     charset = "ISO-8859-1"
Content-Transfer-Encoding: 8bit

<html>
<body>


(These examples have been "sampled" from an incoming bulk email I was sent   )

Translated ... means that you've probably got a header missing and so the boundaries such as you've sent them are illegal .... there's probably an extra call in one of those Perl modules to set it up.   Have a look and if you can't find it post further and I'll look overnight ... won't get a chance this afternoon I'm afraid

Posted by Custard (Custard), 10 May 2005
From memory, the mime boundary can be anything you like as long as it's unique (within the realm of the encapsulating email).

Can we see a dump of the headers too, as the original error refers to corrupt headers.

I wonder if your line mime_type = "TEXT" should be something like text/plain.

B



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.
fb58

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho
0