Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl script with a HTML MIME attachment

Status
Not open for further replies.

mstelmac

Technical User
Oct 22, 2003
53
US
Hello everyone. I'm a very novice perl programmer that is trying to generate an automatic email that attaches a file and sends it to a constant recipient. Because of the version of perl that I am forced to use (CQPERL) I cannot make use of the wonderful perl modules like MIME::Lite. I am essentially forced to use the MIME functions with an HTML email. This script does not populate the body of the message nor attaches a file. However the email goes through fine :) Any help would be appreciated.

-Matt
LM


#!perl -w

# THIS SCRIPT SENDS AN EMAIL WITH AN ATTACHMENT.

use Net::SMTP;

# Connect to the server
my $smtp = Net::SMTP->new("smtp_server_address", Debug => 1);
die "Couldn't connect to server" unless $smtp ;

#my $msg = "<b>Test text";


$smtp->mail("person\@person.com");
$smtp->to("person\@person.com");

$smtp->data();

#Send the header
$smtp->datasend("To: person\@person.com\n");
$smtp->datasend("Subject: Daily Defect Report\n");

#Start the multipart email message
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-Type: multipart/mixed; boundary='12345'\n");

#Create the body of the email message
$smtp->datasend("--12345\n");
$smtp->datasend("Content-Type: text/html\n");
$smtp->datasend("<b>This is a test of sending a text attachment.\n");

#Attach a .txt file to the email message
$smtp->datasend("--12345\n");
$smtp->datasend("Content-Type: application/octet-stream; name=output.txt\n");
$smtp->datasend("Content-Disposition: attachment; filename='C:eek:utput.txt'\n");
$smtp->datasend("Content-Transfer-Encoding: base64\n");
$smtp->datasend("\n");

#End the multipart message
$smtp->datasend("--12345--\n");

#Send the termination string
$smtp->dataend();

$smtp->quit;
 
Code:
#$smtp->datasend("MIME-Version: 1.0\n");
#$smtp->datasend("Content-Type: multipart/mixed; boundary='12345'\n");

#Create the body of the email message
#$smtp->datasend("--12345\n");
Try commenting out these three lines, IIRC the first header type should be a HTML Content Header. I'll have a play about with it, should have some code here somewhere ...

Just to clarify, do you want to send a HTML mail, or an attachment?

--Paul


Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thanks Paul I did what you said and started commenting out lines and I got it to work. Now all I have to do is get the multipart functions to work. The most important part is that I am able to send a text file that contains data. Including a message in the email body is secondary.

Thanks for your help.

-matt
 
Why not just wrap the contents of the text file in <pre></pre> tags, that would preserve the formatting.

Just a suggestion, can't find code at present ...

--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
This scipt works fine paul....use it if you can. I am going to start working on how to add a message body to the email.
thanks for your help.
-Matt







#!perl -w

#############################################################################
#
# The purpose of this script is to produce a text file of all records updated
# in the last 7 days and email it to customer so they can import it into their
# database.
# by: Kelly and Matt
#
#############################################################################

#
# The below portion of the script produces a text file of all records that have
# been updated in the last 7 days.
#

# GET SYSTEM DATE AND SUBTRACT 8 DAYS
@f = (localtime(time-691200))[3..5];
my $date = sprintf "%d-%d-%d", $f[1] +1, $f[0], $f[2] + 1900;
print "$date";

#
# The below portion of the script sends an email to customer that
# contains a file of records updated in last 7 days.
#

use Net::SMTP;

# Connect to the server
my $smtp = Net::SMTP->new("server", Debug => 1);
die "Couldn't connect to server" unless $smtp ;

$smtp->mail("myname\@isp.com");
$smtp->to("myname\@isp");

$smtp->data();

# Send the header
$smtp->datasend("To: customer.com\n");
$smtp->datasend("From: DATABASE\@isp.com\n");
$smtp->datasend("Subject: RECORDS UPDATED SINCE $date\n");

# Start the multipart email message
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-Type: application/octet-stream; name=output.txt\n");
$smtp->datasend("Content-Disposition: attachment; filename=output.txt\n");
$smtp->datasend("Content-Transfer-Encoding: 7bit\n");

# Push whats in the output file into to attachment file
my $file = "c:eek:utput";
open(KM, $file);
my @data = <KM>;

$smtp->datasend("@data\n");

# Send the termination string
$smtp->dataend();

$smtp->quit;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top