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.
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.