Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Using MIME::Lite in Windows to send email with a zip attachment (http://www.velocityreviews.com/forums/t887520-using-mime-lite-in-windows-to-send-email-with-a-zip-attachment.html)

James 08-04-2004 08:53 PM

Using MIME::Lite in Windows to send email with a zip attachment
 
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.

Paul Lalli 08-04-2004 09:58 PM

Re: Using MIME::Lite in Windows to send email with a zip attachment
 
On Wed, 4 Aug 2004, James wrote:

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


<snip>

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


All times are GMT. The time now is 06:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.