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

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

xmaverick
User

Aug 17, 2009, 11:19 AM

Post #1 of 37 (8502 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 subject and body message
my \$subject = 'A message with 2 parts ...';
my \$message_body = "Here's the attachment file(s) you wanted";

my \$your_file_pdf = 'test.pdf';

### Create the multipart container
\$msg = MIME::Lite->new (
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";

\$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 (8499 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 (8492 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 (8480 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',
Filename =>'Sunset.jpg');

\$msg->attach( Type =>'image/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 (8477 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 (8475 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',
Filename =>'Sunset.jpg');

\$msg->attach( Type =>'image/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 (8474 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 (8466 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 (8461 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 (8459 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 (8456 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 (8453 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 (8449 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 (8447 views)
 Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post
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 (8445 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 (8438 views)
 Re: [xmaverick] 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.

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 (8427 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 (8425 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 (8423 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 (8419 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 (8401 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 (8398 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 (8394 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 (8389 views)
 Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post

xmaverick
User

Aug 31, 2009, 7:01 PM

Post #25 of 37 (8375 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 \$?;

FishMonger
Veteran / Moderator

Sep 1, 2009, 6:36 AM

Post #26 of 37 (3007 views)
 Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post
Try this:

 Code
`#!/usr/bin/perl  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 `uuencode \$body`;  close \$pipe;`

(This post was edited by FishMonger on Sep 1, 2009, 6:36 AM)

xmaverick
User

Sep 1, 2009, 7:21 AM

Post #27 of 37 (3004 views)
 Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post
Wouldn't i have to specify the file path and attachment on the open my \$pipe section of the script?

xmaverick
User

Sep 3, 2009, 7:25 AM

Post #28 of 37 (2999 views)
 Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post
I modified the code like this:

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

my \$attachment = "\$baseDir/message.txt";

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

my \$cmd = "uuencode \$attachment |mailx, '-s',\$subj, '-c', \$cc, \$to, "-f\$from"";
system ("\$cmd");

unfortunately i get errors like this:

email.pl: syntax error at line 19: `system' unexpected

FishMonger
Veteran / Moderator

Sep 3, 2009, 7:34 AM

Post #29 of 37 (2998 views)
 Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post
The error message indicates line 19, but the code you posted only has 16 lines, which tells me that it's not the same code you tested when you got that error.

xmaverick
User

Sep 3, 2009, 7:40 AM

Post #30 of 37 (2996 views)
 Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post
It's the same script. It could just be the spaces. I'm using vi editor in the unix box.

FishMonger
Veteran / Moderator

Sep 3, 2009, 7:48 AM

Post #31 of 37 (2995 views)
 Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post
Wrap your code in the code tags so that the formatting is retained.

You have a problem with the quotes on the \$cmd assignment. Why are you using the commas in the command?

Try this:
 Code
`my \$cmd = qq(uuencode \$attachment |mailx -s \$subj -c \$cc \$to -f \$from);`

(This post was edited by FishMonger on Sep 3, 2009, 7:49 AM)

xmaverick
User

Sep 3, 2009, 8:09 AM

Post #32 of 37 (2990 views)
 Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post
Here is how the code looks like now:

#!/usr/bin/perl -w

use strict;
use warnings;

my \$attachment = "\$baseDir/message.txt";

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

my \$cmd = qq(uuencode \$attachment |mailx -s \$subj -c \$cc \$to -f \$from);
system ("\$cmd");

Here is the error now:
email.pl: syntax error at line 18: `(' unexpected

Strange!!!

FishMonger
Veteran / Moderator

Sep 3, 2009, 8:21 AM

Post #33 of 37 (2989 views)
 Re: [xmaverick] Sending Attachment using MIME::Lite [In reply to] Can't Post
What version of perl are you using i.e., what is the output of this command?

 Quote
perl -v

xmaverick
User

Sep 3, 2009, 8:25 AM

Post #34 of 37 (2986 views)
 Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post
I get the following:

This is perl, v5.8.4 built for sun4-solaris-64int
(with 32 registered patches, see perl -V for more detail)

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to the

KevinR
Veteran

Sep 3, 2009, 9:42 AM

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

http://www.unix.com/shell-programming-scripting/118486-email-attachment-script.html
-------------------------------------------------

FishMonger
Veteran / Moderator

Sep 3, 2009, 11:15 AM

Post #36 of 37 (2977 views)
 Re: [KevinR] Sending Attachment using MIME::Lite [In reply to] Can't Post
Thanks Kevin

xmaverick
User

Sep 4, 2009, 9:18 AM

Post #37 of 37 (2968 views)
 Re: [FishMonger] Sending Attachment using MIME::Lite [In reply to] Can't Post
Yeah , i posted my question on another forum for other suggestions. I modified my script even more and now it looks like this and it works.

#!/usr/bin/perl -w

use strict;
use warnings;

my \$attachment = 'something.jpg';

my \$from = 'xxx@domain.com';
my \$to = 'xxx@domain.com';
my \$cc = 'xxx@domain.com';
my \$subj = 'Testing';
my \$body = 'TEST';

my \$cmd = "uuencode \$attachment \$attachment | mailx -r \$from -s \$subj -c \$cc \$to";
system ("\$cmd);

I finally got the code to send attachments. Now, i'm trying to figure out how to include the body into my statement.

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0