Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Trouble with sending more than one attachment

by ytjPerl (Scribe)
on Oct 18, 2020 at 14:44 UTC ( [id://11122981]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am able to send single attachment but not multiple attachments. Can Any one help? The following code only attached one file from the directory

my $msg = MIME::Lite->new ( From => $msgFrom, To => $msgTo, Subject => $msgSubject, Type =>'multipart/alternative', ) or die "Error creating multipart container: $!\n"; # Create the HTML part my $html_part = MIME::Lite::->new( 'Type' => 'multipart/related', ); $html_part->attach( 'Type' => 'text/html', 'Data' => qq{ <body> <p>hello</p> </body> }, ); my $att_part; my @files = glob("F:/folder/*"); $att_part = MIME::Lite::->new( 'Type' => 'application/octet-stream', 'Encoding' => 'base64', 'Path' => shift @files, ); $msg->attach($html_part); $msg->attach($att_part); my $email = $msg->as_string(); my $smtp = Net::SMTP_auth->new($smtphost, Port=>$smtpport) or die "Can +'t connect"; $smtp->mail($msgFrom) or die "Error:".$smtp->message(); $smtp->recipient($msgTo) or die "Error:".$smtp->message(); $smtp->data() or die "Error:".$smtp->message(); $smtp->datasend($email) or die "Error:".$smtp->message(); $smtp->dataend() or die "Error:".$smtp->message(); $smtp->quit or die "Error:".$smtp->message();

Replies are listed 'Best First'.
Re: Trouble with sending more than one attachment
by Corion (Patriarch) on Oct 18, 2020 at 15:29 UTC

    Where in your code is the loop where you loop over the contents of @files?

    You may want something like:

    for my $file (@files) { my $att_part = MIME::Lite::->new( 'Type' => 'application/octet-stream', 'Encoding' => 'base64', 'Path' => $file, ); $msg->attach($att_part); };
      Tried. This does not work

        Maybe you want to tell us in what sense this does not work?

        I don't really know how it fails for you, so I don't know what you need to change.

Re: Trouble with sending more than one attachment
by AnomalousMonk (Archbishop) on Oct 18, 2020 at 18:15 UTC

    Further to Corion's post:   In the OPed code, in the statement

    $att_part = MIME::Lite::->new( 'Type' => 'application/octet-stream', 'Encoding' => 'base64', 'Path' => shift @files, );
    the shift @files expression takes only one file path, the first, from the @files array for attachment.


    Give a man a fish:  <%-{-{-{-<

      Got it. Do you have any idea of how to attach more than one? Thanks
        you need to loop over each file.
        $att_part = MIME::Lite::->new( 'Type' => 'application/octet-stream', 'Encoding' => 'base64', 'Path' => shift @files, ); $msg->attach($html_part); $msg->attach($att_part); # assuming all files are same type and encoding, for my $file(@files) { $msg->attach( 'Type' => ......, 'Encoding' => ......, 'Path' => $file, ); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11122981]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found