CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Sending Attachment using MIME::Lite

 

First page Previous page 1 2 Next page Last page  View All


xmaverick
User

Aug 17, 2009, 11:19 AM

Post #1 of 37 (8669 views)
Sending Attachment using MIME::Lite Can't Post

 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?
Edit / Delete


KevinR
Veteran


Aug 17, 2009, 12:45 PM

Post #2 of 37 (8666 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

change:


Code
$msg = MIME::Lite->new (


to:


Code
my $msg = MIME::Lite->new (


and change:


Code
) or die "Error adding $file_gif: $!\n";


to:


Code
or die "Error adding the pdf file: $!\n";

-------------------------------------------------


xmaverick
User

Aug 17, 2009, 9:26 PM

Post #3 of 37 (8659 views)
Re: [KevinR] Sending Attachment using MIME::Lite [In reply to] Can't Post

Thank you so much. This actually helped. I managed to execute it.


xmaverick
User

Aug 19, 2009, 8:35 AM

Post #4 of 37 (8647 views)
Re: [KevinR] Sending Attachment using MIME::Lite [In reply to] Can't Post

Hey 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?


KevinR
Veteran


Aug 19, 2009, 10:25 AM

Post #5 of 37 (8644 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

these lines:


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

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


should be:


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

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



You should use "strict" too
-------------------------------------------------


xmaverick
User

Aug 19, 2009, 10:39 AM

Post #6 of 37 (8642 views)
Re: [KevinR] Sending Attachment using MIME::Lite [In reply to] Can't Post

Interesting. I modify the script as the following now and i incorporated your suggestions.

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


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');



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

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




However, i now receive the following error:

SMTP MAIL command failed:
5.7.1 Helo invalid .

at attach1.pl line 31


In the script line 31 is the following:

$msg ->send();


What do you think?


KevinR
Veteran


Aug 19, 2009, 10:47 AM

Post #7 of 37 (8641 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

To me it looks like an SMTP server issue, but I don't know what you can do to fix it.
-------------------------------------------------


xmaverick
User

Aug 20, 2009, 8:36 PM

Post #8 of 37 (8633 views)
Re: [KevinR] Sending Attachment using MIME::Lite [In reply to] Can't Post

I don't think its a problem with the SMTP server. The reason i say that is because i can send emails using the script if i modify it to send within our local network. The problem is that i can't make the script send emails outside our network to other servers like gmail or yahoo or hotmail.


FishMonger
Veteran / Moderator

Aug 21, 2009, 2:54 AM

Post #9 of 37 (8628 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

Are you saying that it fails when the recipient is on one of those domains, or when you point to an smtp server on those domains?

If the smtp server is outside of your domain, you almost always need to authenticate before it will allow you to relay emails.

Try it like this:

Code
MIME::Lite->send('smtp', $SMTP_SERVER, AuthUser=>$user, AuthPass=>$pass);



xmaverick
User

Aug 21, 2009, 7:37 AM

Post #10 of 37 (8626 views)
Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post

Yes, it fails when the recipient is on one of those domains. For example if i modify the script and include a gmail, yahoo, or hotmail domain and also include our domain. The script will send to all i list except the gmail, yahoo, or hotmail domain.


I'll set it up the way your suggesting and see what happens.


FishMonger
Veteran / Moderator

Aug 21, 2009, 8:07 AM

Post #11 of 37 (8623 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

Are you able to send emails to those users that are outside of your domain with an email client such as Outlook? If not, then it looks like either your smtp server is blocking the emails or your domain might be on a public blacklist and the recipient smtp servers are doing the blocking.


xmaverick
User

Aug 21, 2009, 8:15 AM

Post #12 of 37 (8620 views)
Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post

Yes, i tested that and it works.


FishMonger
Veteran / Moderator

Aug 21, 2009, 9:09 AM

Post #13 of 37 (8616 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

What did you test, using authentication in the MIME::Lite send method or sending the email via outlook?


KevinR
Veteran


Aug 21, 2009, 9:15 AM

Post #14 of 37 (8614 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post


In Reply To
Yes, i tested that and it works.


You might try changing the subject line, "multiple attachments" to something else, like "my email test" and see if that produces different results.
-------------------------------------------------


xmaverick
User

Aug 21, 2009, 9:23 AM

Post #15 of 37 (8612 views)
Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post

I tested sending an email with my outlook application to a different domains and it works.


Question i'm running the script from a unix box. Will the code below work if i implement?

MIME::Lite->send('smtp', $SMTP_SERVER, AuthUser=>$user, AuthPass=>$pass);


I was under the impression that using the code above was for reason of using WIN 32 systems.


FishMonger
Veteran / Moderator

Aug 21, 2009, 2:03 PM

Post #16 of 37 (8605 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post


In Reply To
I tested sending an email with my outlook application to a different domains and it works.


Question i'm running the script from a unix box. Will the code below work if i implement?

MIME::Lite->send('smtp', $SMTP_SERVER, AuthUser=>$user, AuthPass=>$pass);


I was under the impression that using the code above was for reason of using WIN 32 systems.


The MIME::Lite->send(...) method is used anytime you need/want to specify the smtp server. If not specified, it defaults to using sendmail on the local box. Windows doesn't normally run sendmail, which why it's specified on those systems. However, you can install "sendmail clones" on windows and it will work the same as it does on *nix systems.


xmaverick
User

Aug 24, 2009, 3:59 PM

Post #17 of 37 (8594 views)
Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post

If i use

MIME::Lite->send('smtp', $SMTP_SERVER, AuthUser=>$user, AuthPass=>$pass);



Does this require me to install the Net::SMTP module.
Doesn't that strip of code call upon that specific module?


FishMonger
Veteran / Moderator

Aug 24, 2009, 5:00 PM

Post #18 of 37 (8592 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

Net::SMTP is a core module that comes with the base install of perl, so it's already on your system.


xmaverick
User

Aug 24, 2009, 5:42 PM

Post #19 of 37 (8590 views)
Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post

FishMonger have you ever seen this error when trying to execute a script for email purposes:


SMTP MAIL command failed:
5.7.1 Helo invalid .

at MIME.pl line 32


I'm going crazy trying to execute this script. I have modified my script several times but no luck. The error keeps complaining about the following line:

my $host = '10.200.104.15';
my $user = 'WMS\elliot_anico';
my $pwd = 'gundamx';

MIME::Lite->send ('smtp',$host,AuthUser=>$user,AuthPass=>$pwd,Timeout=>60) or die $!;
$msg->send;


The username and password are correct. Could this possibly be a problem with our companies email relay capabilities. Afterall, i'm able to send emails using the script within our LAN but if the email is to be sent to another domain it doesn't happen?


(This post was edited by xmaverick on Aug 24, 2009, 6:35 PM)


FishMonger
Veteran / Moderator

Aug 24, 2009, 7:07 PM

Post #20 of 37 (8586 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

Are you sure you're sending the correct username?

'WMS\elliot_anico' does not match the from user in your earlier post and I'd need to check the rfc's to verify this, but I don't think the \ backslash character is valid in an email username.

Try this test.

From the command line, telnet to your smtp server and see if you have the same problem.

Telnet to Port 25 to Test SMTP Communication
http://support.microsoft.com/kb/153119


xmaverick
User

Aug 27, 2009, 9:22 AM

Post #21 of 37 (8568 views)
Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post

Well, it turns out that the server i was trying to execute the script from did not allow email relay. So, i spoke with the system administrator and he enabled it but suggested i use mailx to send off emails with attachments rather then using a perl module.


He said that whatever changes he makes on the server that mailx will accept them automatically and that using a perl module wouldn't. He said that if he made a change then that i would have to modify my script using that module. He said this was for simplicity of maintenance. Does this make sense?


FishMonger
Veteran / Moderator

Aug 27, 2009, 10:29 AM

Post #22 of 37 (8565 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post


Quote
He said this was for simplicity of maintenance.

Translation, he probably doesn't know how to correctly setup the email system.

mailx uses sendmail on the local server to send out the email, which is also the default method used by the MIME::Lite module.

When you specify SMTP in the module's send method, you're normally specifying a remote server (which can be on the same or different network) and the module will connect to it via a socket and that remote server then sends out the email (assuming the relaying is allowed).

Try leaving out the SMTP server/auth stuff and do a simple send. If the server is configured correctly, then the email should get sent out via the local sendmail daemon just as is done with mailx.

So, the send command in your script should look like this:

Code
$msg->send;



xmaverick
User

Aug 28, 2009, 2:39 PM

Post #23 of 37 (8561 views)
Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post

Hey,

Do you know where i can find a perl script that shows how to send an email attachment with mailx. I find one and tweaked it but im not too sure how to add an attachment. Check it out:

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

my $from = 'elliot.anico@cellularatsea.com';
my $to = 'eanicom@hotmail.com';
my $cc = 'eanicom@gmail.com';
my $subj = 'Testing';
my $body = "test.txt";


open my $pipe, '|-', '/usr/bin/mailx', '-s',$subj, '-c', $cc, $to, "-f$from",
or die "can't open pipe to mailx: $!\n";

print $pipe $body ;

close $pipe;

die "mailx exited with a non-zero status: $?\n" if $?;


FishMonger
Veteran / Moderator

Aug 29, 2009, 1:13 PM

Post #24 of 37 (8556 views)
Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

http://www.google.com/search?q=man+mailx+attachment&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a


xmaverick
User

Aug 31, 2009, 7:01 PM

Post #25 of 37 (8542 views)
Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post

Hey,

Would you happen to know how to modify this script in order to send attachments?


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

my $from = 'elliot.anico@cellularatsea.com';
my $to = 'eanicom@hotmail.com';
my $cc = 'eanicom@gmail.com';
my $subj = 'Testing';
my $body = "test.txt";


open my $pipe, '|-', '/usr/bin/mailx', '-s',$subj, '-c', $cc, $to, "-f$from",
or die "can't open pipe to mailx: $!\n";

print $pipe $body ;

close $pipe;

die "mailx exited with a non-zero status: $?\n" if $?;

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives