Send email Perl with attached files and remove file after sent

send_email_file_report.pl

#!/usr/bin/perl

use MIME::Lite;

my $attached_file = $ARGV[0];
my $report_directory = "/mnt/reports";

my $msg = MIME::Lite->new(
 From => 'xxxx@xxxx.com',
 To => 'xxx@xxx.com',
 Cc => 'xxxx@xxxx.com, xxxxx@xxxx.com',
 Bcc => 'xxx@xxx.com, xxx@xxx.com',
 Subject => "$attached_file",
 Type => 'multipart/mixed',
);

$msg->attach(
 Type => 'TEXT',
 Data => "Mensaje : $attached_file \nNota: Este correo ha sido generado automáticamente por favor no responda a este mensaje.",
);

$msg->attach(
 Type => 'text/plain',
 Path => "$report_directory/$attached_file",
 Filename => "$attached_file",
 Disposition => 'attachment'
);

$msg->send or die "Mensaje no enviado";

system("rm -rf $report_directory/$attached_file");


------------ bash execution ------------------------

#!/bin/bash

report_directory="/mnt/reports"
exec_directory="/root/bin"

cd $report_directory
for i in *.csv; do $exec_directory/send_email_file_report.pl $i; done
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s