zentara has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I would like to find a way to send large email attachments, and monitor their progress as they are sent. The popular mail modules, don't seem to have any way to send by "chunks" in a callback. Does anyone know of a way, other than using pure Sockets to do this? Thanks.

I'm not really a human, but I play one on earth. flash japh
  • Comment on Sending mail with a callback to track progress

Replies are listed 'Best First'.
Re: Sending mail with a callback to track progress
by derby (Abbot) on Aug 25, 2005 at 12:47 UTC
    Hmmm ... not that i know of but what's your concern? Is the data computed on the fly or in files? If its files and you're worried about memory consumption, don't be. Using MIME::Lite, a snippet such as this:

    #!/usr/bin/perl -wd # use MIME::Lite; ### Create a new multipart message: $msg = MIME::Lite->new( From => 'foo@bar.com', To => 'bar@foo.com', Subject => 'A message with 2 parts...', Type => 'multipart/mixed' ); $msg->attach( Type => 'TEXT', Data => "Here's the file you wanted" ); $msg->attach( Type => 'text/plain', Path => 'baz.txt', Filename => 'baz.txt', Disposition => 'attachment' ); $msg->send;

    is pretty efficient. The file is not processed until the send method is called and then it's pushed across the sendmail pipe (or whatever mailer your using) in 2K chunks

    -derby

    update: looking at the MIME::Lite code, it would be trivial to implement a callback feature. You need to modify the build method to not reject the key and the print_simple_body method to utilize the callback. Then you could do something like this:

    $msg->attach( Type => 'text/plain', Path => 'foo.txt', Filename => 'foo.txt', Disposition => 'attachment', Callback => \&hashit, ); $msg->send; sub hashit { print STDERR "#"; }
Re: Sending mail with a callback to track progress
by grinder (Bishop) on Aug 25, 2005 at 12:57 UTC
    The popular mail modules, don't seem to have any way to send by "chunks" in a callback

    I'm not quite sure what you're trying to do here. SMTP is a one shot affair. You negotiate the envelope and then send the entire message after the DATA command. There's no "chunking" going on.

    Do you mean that you're having trouble holding the entire body in memory (e.g. as a scalar)?

    If that's the case, something low-level like Net::SMTP will do the trick. What you want to do is build up your MIME message and write it out to a file. Then you set up your N::S object by emitting the appropriate headers, and the heart of the matter is something like:

    my $smtp = Net::SMTP->new; ... $smtp->data(); open BODY, '<', 'message.mime' or die "bleah: $!\n"; while( <BODY> ) { chomp; $smtp->datasend( $_ ); } $smtp->dataend(); $smtp->quit();

    Since you just wrote your file, you then know how many lines it contains. In the above loop, you can compare the number of lines read to the total number of lines and thereby calculate your progress through the file and display it as a percentage for your overworked postal worker.

    Is that what you meant?

    - another intruder with the mooring in the heart of the Perl

Re: Sending mail with a callback to track progress
by gargle (Chaplain) on Aug 25, 2005 at 12:32 UTC
    I don't know of any module which does this. Isn't it better to publish your large document on a public server and let your users search the document? Why would you want to monitor your upload? You'd only monitor the connection with your mail server, nothing more.

    BTW, I dread long attachments which I'm always unable to receive because of a full mailbox.
Re: Sending mail with a callback to track progress
by ait (Hermit) on Aug 25, 2005 at 12:59 UTC
    You might find something on CPAN specific for your MTA. For example, This qmail queue library helps you inject stuff directly into the qmail queue, bypassing all the other overhead.
Re: Sending mail with a callback to track progress
by zentara (Cardinal) on Aug 25, 2005 at 15:54 UTC
    Thanks for all the tips. What I'm doing is making a simple one step "video blogger / video email" script for linux using Tk and mplayer. The script works fine making and previewing the mpeg, and I have code to upload it(~500k to 1 meg) automatically to a website showing progress, but when emailing the script, it just sits there doing the upload with no indication of how far along it has gone. Maybe I'll just live with it being a video blogger, since most people don't want big email attachments anyways.

    I'm not really a human, but I play one on earth. flash japh
Re: Sending mail with a callback to track progress
by ait (Hermit) on Aug 25, 2005 at 17:51 UTC
    Hi I posted this today. I think you will enjoy it: 486665

    Have fun!

    --
    Alejandro