How to attach file in email?

1

I have this Perl code 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 anyone 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);
    
asked by anonymous 20.06.2015 / 22:16

1 answer

1

For this I believe you will have to use the MIME::Lite module, download it at link

  

Note: If the link is out of date, go here link and search with Ctrl + F by 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 attachment:

#!/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: link

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);
    
21.06.2015 / 02:16