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

Hello Monks,

I have a question about Net::SMTP module... now I know what you tinking, why don't I use MIME:Lite or such? Well, because the script will be run from the server and the only available module is Net::SMTP.
With that out of the way, on to the code:

my $smtp = Net::SMTP->new($host); $smtp->mail($myemail); $smtp->to(@emails); # Start the mail $smtp->data(); my $dispName = "Test Attachment"; # Send the header. $smtp->datasend("To: Myself\n"); $smtp->datasend("From: Me\n"); $smtp->datasend("Reply-To: $myemail\n"); $smtp->datasend("Subject: Test email\n"); $smtp->datasend("Content-Disposition: attachment; filename= $dispName\ +n"); ???? Is this right ??? $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend('Content-Type: multipart/mixed; boundary= "--*B*--"\n\ +n'); ??? Is this right ???? # Send the body. $smtp->datasend("--*B*--\n"); $smtp->datasend("Content-Type: text/plain\n"); $smtp->datasend("This is a test email\n\n"); # Send attachemnt my $buffer = ""; my $file = "Test.txt"; open (MAIL, "<$file") or die "ERROR: Could not open email file"; while (<MAIL>) {$buffer .= $_} close (MAIL); $smtp->datasend("--*B*--\n"); $smtp->datasend("Content-Type: application/text; name= \"$file\" \n"); + ???? Is this right???? $smtp->datasend("\n"); $smtp->datasend("$buffer\n\n"); # Finish sending the mail $smtp->dataend(); # Close the SMTP connection $smtp->quit;

What this gives me is some file (with unrecognized extension) as an attachemtn, and no text in the body (I think the body went into the file)

So, wherever you see "??? Is that right ???" is where the problems are likely to be comming from... since I don't fully understand how those lines work.

Any help will be appreciated!


P.S. what I am trying to do is send some text in the body of the message, as well as a text file as attachemnt

Replies are listed 'Best First'.
Re: Net::SMTP format
by samtregar (Abbot) on Mar 12, 2008 at 15:40 UTC

    I see one obvious problem:

    $smtp->datasend('Content-Type: multipart/mixed; boundary="--*B*--"\n\n +');

    You're using single quotes and trying to include escape sequences. Those won't be processed and you'll end up with raw \n in your message. Instead:

    $smtp->datasend(qq{Content-Type: multipart/mixed; boundary="--*B*--"\n +\n});

    As far as getting the rest of the MIME encoding right you're going to have to read the specs or copy the code. Personally I'd work on figuring out how to get MIME::Lite installed on the server instead.

    -sam

      It all turned out to be simpler than I thought.
      Just remember: LEAVE A BLANK LINE AFTER "Content Type: " setting
      Simple as that. So, here's the working code for sending text along with an attached file:

      my $smtp = Net::SMTP->new($host); $smtp->mail($myemail); $smtp->to(@emails); # Start the mail $smtp->data(); # Send the header. There needs to be a newline at the end of each line $smtp->datasend("To: Myself\n"); $smtp->datasend("From: Me\n"); $smtp->datasend("Reply-To: oakulov\@rim.com\n"); $smtp->datasend("Subject: Test email\n"); $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("Content-Type: multipart/mixed; boundary= \"*BCKTR*\"\ +n\n"); # Send the body. $smtp->datasend("--*BCKTR*\n"); $smtp->datasend("Content-Type: text/plain\n\n"); # this is a formatti +ng command, so leave an extra line between it and the text $smtp->datasend("This is a test email\n"); # Send attachemnt my $buffer = ""; my $file = "Test.txt"; open (MAIL, "<$file") or die "ERROR: Could not open email file"; while (<MAIL>) {$buffer .= $_} close (MAIL); $smtp->datasend("--*BCKTR*\n"); $smtp->datasend("Content-Type: text/plain; name= \"$file\" \n"); $smtp->datasend("Content-Disposition: attachment; filename= \"$file\"\ +n\n"); $smtp->datasend("$buffer\n\n"); # Send the END section break $smtp->datasend("--*BCKTR*--\n\n"); # Finish sending the mail $smtp->dataend(); # Close the SMTP connection $smtp->quit;
Re: Net::SMTP format
by NetWallah (Canon) on Mar 12, 2008 at 15:33 UTC
    Please read this article from the Perl Journal.

    The bottom line recommendation is that you should include the code from Mime::Lite (Lite.pm) into your code, or, preferably, upload it in such a way that it is accessible from your code.

    Here is another perlmonks thread that discusses a similar issue.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom