Using MIME::Lite in Windows to send email with a zip attachment

J

James

I'm not a hardcore perl master, and I spent an inordinate amount of
time trying to figure out how to send an email with a zip file
attachment. The documentation was confusing and Google didn't help,
so I'm posting this for the next guy.

This worked using Windows 2000 Professional, ActiveState Perl v5.6.1,
and MIME::Lite 3.01:

require MIME::Lite;

#
# Set up a hash with all the arguments. You don't have
# have to do it this way, but if you're writing reusable
# code, you'll be glad you did.
#
my %args = (
From => "myAccount\@mySite.com",
To => "yourAccount\@yourSite.com",
Subject => "Some droll subject",
BodyType => "TEXT",
Message => "Some pithy message",

AttachType => "application/zip",
AttachEncoding => "base64",
AttachFullpath => "D:\\the\\fullpath\\To\\MyFile.zip",
AttachFilename => "MyNewFileName.zip",

Server => "my.mail.server.com"
);

#
# Create the main body
#
my $mime_msg = MIME::Lite->new(
From => $args{From},
To => $args{To},
Subject => $args{Subject},
Type => $args{BodyType},
Data => $args{Message}
);

#
# Now add the attachment
#
$mime_msg->attach(
Type => $args{AttachType},
Encoding => $args{AttachEncoding},
Path => $args{AttachFullpath},
Filename => $args{AttachFilename},
Disposition => "attachment"
);

#
# Now send it.
#
$mime_msg->send('smtp', $args{Server}, Timeout=>60);


That's it. The very last line was the key. I couldn't find
it anywhere in the documentation or Google. The default:

$mime_msg->send()

uses sendmail instead of smtp, which doesn't exist in my setup.

The Disposition attribute can be important if you're sending
something like an html file. When I attached an html file without
specifying the Disposition, (using Outlook) I always ended up with
the html as the main message, and the "main body" I expected became
the attachment.
 
P

Paul Lalli

I'm not a hardcore perl master, and I spent an inordinate amount of
time trying to figure out how to send an email with a zip file
attachment. The documentation was confusing and Google didn't help,
so I'm posting this for the next guy.

$mime_msg->send('smtp', $args{Server}, Timeout=>60);


That's it. The very last line was the key. I couldn't find
it anywhere in the documentation or Google. The default:

$mime_msg->send()

uses sendmail instead of smtp, which doesn't exist in my setup.

Not that I don't think the next guy will appreciate it, but I'm curious
what documentation you read that you couldn't find this.

From MIME::Lite's docs, as found on CPAN:
----------------------------------------------------------------
send HOW, HOWARGS...

Class/instance method. This is the principal method for sending mail,
and for configuring how mail will be sent.

As a class method with a HOW argument and optional HOWARGS, it sets
the default sending mechanism that the no-argument instance method will
use. The HOW is a facility name (see below), and the HOWARGS is
interpreted by the facilty. The class method returns the previous HOW and
HOWARGS as an array.

<...>

There are three facilities:

"sendmail", ARGS...

Send a message by piping it into the "sendmail" command. Uses the
send_by_sendmail() method, giving it the ARGS. This usage implements (and
deprecates) the sendmail() method.

"smtp", [HOSTNAME]

Send a message by SMTP, using optional HOSTNAME as SMTP-sending host.
Uses the send_by_smtp() method.

"sub", \&SUBREF, ARGS...

Sends a message MSG by invoking the subroutine SUBREF of your
choosing, with MSG as the first argument, and ARGS following.
----------------------------------------------------------------


Did you not see this, or not understand this? I don't mean that to be
sarcastic. I'm just wondering if someone might want to suggest updates or
changes to the docs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top