How to attach file in email?

Asked

Viewed 265 times

1

I have this code in Perl that sends emails from a list with subject and body, I need one that does this but it is possible to attach some file of my choice. Can someone help me?

#!/usr/local/bin/perl

$ARGC=@ARGV;
if ($ARGC !=4) {
   printf "Você digitou de uma forma errada. Siga as instruções.\n";
   printf "INSTRUÇÕES - UND3F1N3D\n";
   printf "perl $0 <mailist> <remetente\@mail.com> <assunto> <corpo.html>\n";
   printf "Exemplo: perl $0 lista01.txt peu\@msn.com Ola index.html\n";
   exit(1);
}

$mailtype = "content-type: text/html";
$sendmail = '/usr/sbin/sendmail';
$sender = $ARGV[1];
$subject = $ARGV[2];
$efile = $ARGV[0];
$emar = $ARGV[0];
open(FOO, $ARGV[3]);
@foo = <FOO>;
$corpo = join("\n", @foo);
open (BANDFIT, "$emar") || die "Can't Open $emar";
$cont=0;

while(<BANDFIT>) {
   ($ID,$options) = split(/\|/,$_);
   chop($options);
   foreach ($ID) {
      $recipient = $ID;
      open (SENDMAIL, "| $sendmail -t");
      print SENDMAIL "$mailtype\n";
      print SENDMAIL "Subject: $subject\n";
      print SENDMAIL "From: $sender\n";
      print SENDMAIL "To: $recipient\n\n";
      print SENDMAIL "$corpo\n\n";
      close (SENDMAIL);
      $cont=$cont+1;
      printf "$cont Enviado para $recipient";
   }
}
close(BANDFIT);

1 answer

1


For this I believe you will have to use the module MIME::Lite, download him into http://www.cpan.org/authors/id/R/RJ/RJBS/MIME-Lite-3.030.tar.gz

Note: if the link is out of date access here http://search.cpan.org/~rjbs/ and look with Ctrl+F for MIME-LITE)

Command to compile and install:

$tar xvfz MIME-Lite-3.030.tar.gz
$cd MIME-Lite-3.030
$perl Makefile.PL
$make
$make install

Dependencies

To use MIME-LITE you need to install the following modules:

A simple example of an annex:

#!/usr/bin/perl
use MIME::Lite;

$to = '[email protected]';
$cc = '[email protected]';
$from = '[email protected]';
$subject = 'email de teste';
$message = 'Corpo do email de teste';

$msg = MIME::Lite->new(
                 From     => $from,
                 To       => $to,
                 Cc       => $cc,
                 Subject  => $subject,
                 Type     => 'multipart/mixed'
                 );

# Add your text message.
$msg->attach(Type         => 'text',
             Data         => $message
            );

# Specify your file as attachement.
$msg->attach(Type        => 'image/gif', #Mimetype do arquivo
             Path        => '/tmp/logo.gif', #Pasta do arquivo
             Filename    => 'logo.gif', #Nome do arquivo
             Disposition => 'attachment'
            );       
$msg->send;
print "Email enviado com sucesso\n";

Documentation: http://search.cpan.org/~rjbs/MIME-Lite-3.030/lib/MIME/Lite.pm

Modifying your existing code:

#!/usr/local/bin/perl
use MIME::Lite;

$ARGC=@ARGV;
if ($ARGC !=4) {
   printf "Você digitou de uma forma errada. Siga as instruções.\n";
   printf "INSTRUÇÕES - UND3F1N3D\n";
   printf "perl $0 <mailist> <remetente\@mail.com> <assunto> <corpo.html>\n";
   printf "Exemplo: perl $0 lista01.txt peu\@msn.com Ola index.html\n";
   exit(1);
}

$sendmail = '/usr/sbin/sendmail';
$sender = $ARGV[1];
$subject = $ARGV[2];
$efile = $ARGV[0];
$emar = $ARGV[0];
open(FOO, $ARGV[3]);
@foo = <FOO>;
$corpo = join("\n", @foo);
open (BANDFIT, "$emar") || die "Can't Open $emar";
$cont=0;

while(<BANDFIT>) {
   ($ID,$options) = split(/\|/,$_);
   chop($options);
   foreach ($ID) {
      $recipient = $ID;
        $msg = MIME::Lite->new(
                         From     => $sender,
                         To       => $recipient,
                         Subject  => $subject,
                         Type     => 'multipart/mixed'
                         );

        # Add your text message.
        $msg->attach(Type         => 'text/html',
                     Data         => $corpo
                    );

        # Specify your file as attachement.
        $msg->attach(Type        => 'image/gif', #Mimetype do arquivo
                     Path        => '/tmp/logo.gif', #Pasta do arquivo
                     Filename    => 'logo.gif', #Nome do arquivo
                     Disposition => 'attachment'
                    );       
        $msg->send;

      $cont=$cont+1;
      printf "$cont Enviado para $recipient";
   }
}
close(BANDFIT);
  • Good afternoon William, in this example would send 1 email at a time and not serve me, I will try to join the two, but n understand anything perl. Still, thank you.

  • But wouldn’t that be more of an attachment instead of several emails? The first code loads the emails from a mailing list.

  • Thank you man, I’m going to test here... broke a tree hahah

  • me returned this error [root@b 04s12 ~]# perl envio.pl lts.txt "[email protected]" "Test" test.html Can’t locate Object method "new" via package "MIME::Lite" (Perhaps you forgot to load "MIME::Lite"?) at upload.pl line 28, <BANDFIT> line 1.

  • @Kynleonardo My fault, I forgot to add in the example the use MIME::Lite;, note that if you have any doubts look at the first simple example, it can help you. I edited the answer, just add use MIME::Lite; after the #!/usr/local/bin/perl

  • It returned another error Can’t locate Email/Date/Format.pm in @INC (@INC contains: /usr/local/lib64/Perl5 /usr/local/share/Perl5 /usr/lib64/Perl5/vendor_perl /usr/share/Perl5/vendor_perl /usr/lib64/Perl5 /usr/share/Perl5 .) at /usr/local/share/Perl5/MIME/Lite.pm line 1100, <BANDFIT> line 1.

  • @Kynleonardo is required to install some dependencies in the MIME::Types, Mail::Address, MIME::Base64, MIME::Quotedprint, Net:::SMTP and Email:Date:::Format. See I edited the answer and added the links from these dependencies. I hope it helps.

  • I will test, obg again... As soon as I can, I will give feedback

  • Everything ok, tested and approved... @Guilherme Thank you very much.

  • 1

    To install the suggested module sudo cpan MIME::Lite which deals with everything including the closing of dependencies. (+1)

  • 1

    @Thanks, I have custom to use make, sometimes I don’t even imagine that there are shortcuts :) It seems very useful.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.