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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send E-Mail with Mime::Lite & NET::SMTP.

Status
Not open for further replies.

zincyo

Programmer
Jun 13, 2007
35
0
0
US
I am currently trying to create a batch file which executes several different perl scripts. The last perl script which I need to create will be executed by the batch file, and will automatically attach and e-mail the new "recon.xls" file.

I know that I need to use the Mime::Lite & Net::SMTP packages, however i'm not sure how to send the attchment. This is what I have so far:

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

use MIME::Lite;

$smtp = Net::SMTP->new('mail.whatever.com);

# set up email
$to = "Send_address@whatever.com";
$from = "me@whatever.com";
$subject = "Recon File";
$message = "Attached is the Recon File";
$file = "Newest Recon.xls";


# create a new message
$msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Data => $message
);


Any help would be greatly appreciated!
 
Here is a snipped of code that I use to send attachments to an array of email addresses. Perhaps there is something in here that will help.

#######################################################################
#######################################################################
#
# SEND BULK HTML EMAIL
#
#######################################################################
#######################################################################

sub send_bulk_email
{
use MIME::Lite;
use Net::SMTP;

my $from_address = $EMAIL{'from'};
my $mail_host = 'xxxYOURSMPTHOSTxxx';
my $subject = $FORM{'subject'};
my $message_body = $FORM{'content'};
my $to_address;

$counter = 0;
do {
$counter ++;
$to = pop @email_data;
$send_to = $to;

if ($to ne "")
{
$VALID = "NO";
&validate_address;
if ($VALID eq "YES")
{
if ($to_address)
{ $to_address .= ","; }
$to_address .= "$to";
}
}
} until ($to eq "");

### Create the multipart container
$msg = MIME::Lite->new (
From => $from_address,
Bcc => $to_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";

### Add the text message part
$msg->attach (
Type => 'text/html',
Data => $message_body
) or die "Error adding the text message part: $!\n";


#######################################################################
#######################################################################
#
# ATTACHMENT TYPE DEFINITONS
#
#######################################################################
#######################################################################

if ($FORM{'UploadFile'})
{
($MIME_FileName, $C_one, $void) = split (/\./, $FORM{'UploadFile'});

if ($C_one eq "doc") { $MIME_TYPE = "application/msword"; }
elsif ($C_one eq "xls") { $MIME_TYPE = "application/excel"; }
elsif ($C_one eq "pps") { $MIME_TYPE = "application/mspowerpoint"; }
elsif ($C_one eq "ppt") { $MIME_TYPE = "application/mspowerpoint"; }
elsif ($C_one eq "zip") { $MIME_TYPE = "application/zip"; }
elsif ($C_one eq "pdf") { $MIME_TYPE = "application/pdf"; }
elsif ($C_one eq "jpg") { $MIME_TYPE = "image/jpg"; }
elsif ($C_one eq "gif") { $MIME_TYPE = "image/gif"; }
else { dienice("Unknown File Type.");}

my $my_file = "/var/ my $your_file = "$FORM{'UploadFile'}";

$msg->attach (
Type => $MIME_TYPE,
Path => $my_file,
Filename => $your_file,
Disposition => 'attachment'
) or die "Error adding attachment to email: $!\n";
}

#######################################################################
#######################################################################
#
# SEND THE EMAIL
#
#######################################################################
#######################################################################

### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;

if ($FILE{'UploadFile'})
{
unlink("/var/ }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top