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

My script uses CGI.pm to upload a user-selected file to the website server, followed by MIME::Lite to e-mail the file to a recipient. If I use $msg->send to mail the attached file using 'sendmail', is it safe to delete the uploaded file from the server immediately afterwards?
  • Comment on Synchronisation between sending mail attachments with MIME::Lite and deleting the attached files
  • Download Code

Replies are listed 'Best First'.
Re: Synchronisation between sending mail attachments with MIME::Lite and deleting the attached files
by ikegami (Patriarch) on Aug 12, 2009 at 18:21 UTC

    When using sendmail, MIME::Lite opens a pipe to sendmail and does $self->print( \*SENDMAIL );. As you can see from the following, that sends the content of the file (as opposed to having sendmail read the file):

    use strict; use warnings; use File::Basename qw( basename ); use MIME::Lite qw( ); my $msg = MIME::Lite->new( From => 'me@myhost.com', To => 'you@yourhost.com', Cc => 'some@other.com, some@more.com', Subject => 'A message with 2 parts...', Type => 'multipart/mixed' ); $msg->attach( Type => 'TEXT', Data => "Here's the file you wanted" ); $msg->attach( Type => 'application/perl', Path => $0, Filename => basename($0), Disposition => 'attachment' ); $msg->print();
    Content-Transfer-Encoding: binary Content-Type: multipart/mixed; boundary="_----------=_125010106643850" MIME-Version: 1.0 X-Mailer: MIME::Lite 3.01 (F2.74; T1.27; A1.74; B3.07; Q3.07) Date: Wed, 12 Aug 2009 14:17:46 -0400 From: me@myhost.com To: you@yourhost.com Cc: some@other.com, some@more.com Subject: A message with 2 parts... This is a multi-part message in MIME format. --_----------=_125010106643850 Content-Disposition: inline Content-Length: 26 Content-Transfer-Encoding: binary Content-Type: text/plain Here's the file you wanted --_----------=_125010106643850 Content-Disposition: attachment; filename="a.pl" Content-Transfer-Encoding: base64 Content-Type: application/perl; name="a.pl" dXNlIHN0cmljdDsKdXNlIHdhcm5pbmdzOwoKdXNlIEZpbGU6OkJhc2VuYW1l IHF3KCBiYXNlbmFtZSApOwp1c2UgTUlNRTo6TGl0ZSAgICAgcXcoICk7Cgpt eSAkbXNnID0gTUlNRTo6TGl0ZS0+bmV3KAogICAgRnJvbSAgICAgICAgPT4g J21lQG15aG9zdC5jb20nLAogICAgVG8gICAgICAgICAgPT4gJ3lvdUB5b3Vy aG9zdC5jb20nLAogICAgQ2MgICAgICAgICAgPT4gJ3NvbWVAb3RoZXIuY29t LCBzb21lQG1vcmUuY29tJywKICAgIFN1YmplY3QgICAgID0+ICdBIG1lc3Nh Z2Ugd2l0aCAyIHBhcnRzLi4uJywKICAgIFR5cGUgICAgICAgID0+ICdtdWx0 aXBhcnQvbWl4ZWQnCik7CgokbXNnLT5hdHRhY2goCiAgICBUeXBlICAgICAg ICA9PiAnVEVYVCcsCiAgICBEYXRhICAgICAgICA9PiAiSGVyZSdzIHRoZSBm aWxlIHlvdSB3YW50ZWQiCik7CgokbXNnLT5hdHRhY2goCiAgICBUeXBlICAg ICAgICA9PiAnYXBwbGljYXRpb24vcGVybCcsCiAgICBQYXRoICAgICAgICA9 PiAkMCwKICAgIEZpbGVuYW1lICAgID0+IGJhc2VuYW1lKCQwKSwKICAgIERp c3Bvc2l0aW9uID0+ICdhdHRhY2htZW50JwopOwoKJG1zZy0+cHJpbnQoKTsK Cg== --_----------=_125010106643850--

    That means you can safely delete the file as soon as it has been sent if you don't care about delivery problems.