Connecting Tech Pros Worldwide Forums | Help | Site Map

Sending Attachment using MIME::Lite

Newbie
 
Join Date: Aug 2009
Posts: 2
#1: Aug 17 '09
Hello,

I'm a newbie to perl and i'd like some assistance trying to figure out how to send an attachment using MIME::Lite. I have searched the net far and wide and have used and modify different examples of attachment scripts. Unfortunately, i have had no luck trying to execute my script.


Every time i attempt to execute the script i receive the following response:
Global symbol "$msg" requires explicit package name at attach1.pl line 22.
Global symbol "$msg" requires explicit package name at attach1.pl line 30.
Global symbol "$msg" requires explicit package name at attach1.pl line 36.
Global symbol "$file_gif" requires explicit package name at attach1.pl line 41.
Global symbol "$msg" requires explicit package name at attach1.pl line 43.


Below is the code:

#!/usr/bin/perl -w
use strict;
use warnings;
use MIME::Lite;



### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'elliot.anico@cellularatsea.com';
my $to_address = 'elliot.anico@cellularatsea.com';


### Adjust subject and body message
my $subject = 'A message with 2 parts ...';
my $message_body = "Here's the attachment file(s) you wanted";

### Adjust the filenames
my $my_file_pdf = '/export/home/omcadmin/bin/test.pdf';
my $your_file_pdf = 'test.pdf';

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

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

### Add the PDF file
$msg->attach (
Type => 'pdf/pdf',
Path => $my_file_pdf,
Filename => $your_file_pdf,
Disposition => 'attachment'
) or die "Error adding $file_gif: $!\n";

$msg->send;




I know this is probably something stupid. Can someone assist me with this?

nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#2: Aug 18 '09

re: Sending Attachment using MIME::Lite


When you are using strict pragma, all the local variables in the script need to be declared with my.
Change the line:
Expand|Select|Wrap|Line Numbers
  1. $msg = MIME::Lite->new (
  2.  
  3.  
to:

Expand|Select|Wrap|Line Numbers
  1. my $msg = MIME::Lite->new (
  2.  
  3.  

As for $file_gif, the variable is not defined anywhere in the script.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#3: Aug 18 '09

re: Sending Attachment using MIME::Lite


These are all errors associated with using strict and warnings and need to be worked out to get to any real errors.

Regards,

Jeff
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Aug 18 '09

re: Sending Attachment using MIME::Lite


Already replied to and resolved on another forum.
Newbie
 
Join Date: Aug 2009
Posts: 2
#5: Aug 19 '09

re: Sending Attachment using MIME::Lite


I see. well, i managed to modify my script but i'm having problems attempting to send the email via Net::SMTP. I keep receiving the following error when i execute the script:

SMTP MAIL command failed:
5.7.1 Helo invalid .

at attach1.pl line 30


This is the very last line in the script below which is
$msg -> send;

Below is my modified script:

#!/usr/bin/perl -w
use warnings;
use MIME::Lite;
use Net::SMTP;


my $msg = MIME::Lite->new(
From => 'elliot.anico@cellularatsea.com',
To => 'elliot.anico@cellularatsea.com',
cc => 'wms.nmc@cellularatsea.com',
Subject => 'Multiple attachments',
Type => 'multipart/mixed');

$msg->attach( Type =>'image/jpg',
Path =>'/export/home/omcadmin/bin/Sunset.jpg',
Filename =>'Sunset.jpg');

$msg->attach( Type =>'image/jpg',
Path =>'/export/home/omcadmin/bin/Winter.jpg',
Filename =>'Winter.jpg');

$msg->attach( Type =>'TEXT',
Data =>'This is a test for outside usage');



$SMTP_SERVER = 'wmsexg01.corp.cellularatsea.com';

MIME::Lite->send('smtp', 'SMTP_SERVER');
$msg -> send;



What could i be doing wrong?
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#6: Aug 19 '09

re: Sending Attachment using MIME::Lite


see perlguru
Reply