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

Discussion in 'Perl Misc' started by James, Aug 4, 2004.

  1. James

    James Guest

    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.
    James, Aug 4, 2004
    #1
    1. Advertising

  2. James

    Paul Lalli Guest

    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.
    Paul Lalli, Aug 4, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron Brady

    SQL, lite lite lite

    Aaron Brady, Dec 29, 2008, in forum: Python
    Replies:
    15
    Views:
    749
    Aaron Brady
    Jan 1, 2009
  2. Replies:
    8
    Views:
    406
  3. ecureuil
    Replies:
    0
    Views:
    325
    ecureuil
    May 28, 2006
  4. Replies:
    2
    Views:
    397
    Tintin
    Jan 5, 2007
  5. a
    Replies:
    2
    Views:
    224
    Jens Thoms Toerring
    Sep 4, 2009
Loading...

Share This Page