howto send a mail with attachment via perl script ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
howto send a mail with attachment via perl script ?
hi
in my perl script I write the log with
BEGIN
{
use CGI::Carp qw(carpout);
my $errorlog = "/var/errorlog.txt";
open(LOG, ">$errorlog") or die("Unable to open $errorlog: $!\n");
print LOG "Errors:\n";
carpout(*LOG);
}
and transfer 2 file via ftp
.............................................................................................
.............................................................................................
my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3);
$ftp or die "$server: cannot connect: $@";
# If you don't use ~/.netrc
$ftp->login ('anonymous', 'mail@domain.net') or
die "$_: cannot logon: " . $ftp->message;
# Put file 2 (not 1) to the ftp server
$ftp->put ($f2) or
die "$server: cannot put $f2: " . $ftp->message;
$ftp->quit;
# wait for 10 seconds
sleep (1 * 10);
}
but how to send the mail on the end with the log file errorlog.txt as attachment,
when the transfer was successfully completed ?
I think something like this:
open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "to:$mail\n";
print MAIL "from:$linux\n";
print MAIL "subject:ftp transfer was successfully \n";
print MAIL "------------------------------------------------------------------\n";
print MAIL "ftp transfer was successfully\n\n";
print MAIL "Time: ". localtime(time) ." \n";
print MAIL "------------------------------------------------------------------\n";
The best way to see it is to read the source of a message in your inbox that has an attachment. The trick is to use multipart boundaries. Here is a cut down example from an e-mail sent to me:
Code:
From: xyz@host.com
To: xyz@host.com
Subject: Some subject
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_NextPart_000_002F_01C3F989.5E1B7780"
------=_NextPart_001_0030_01C3F989.5E1B7780
Content-Type: text/plain;
Content-Transfer-Encoding: quoted-printable
This is the message text
below is the binary content of a jpg in a new multipart section
------=_NextPart_000_002F_01C3F989.5E1B7780
Content-Type: image/jpeg; name="file.jpg"
Content-Disposition: attachment; filename="file.jpg"
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEASABIAAD/7Q2AUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAHscAgAAAgAC
HAJ4AG9XaW0gRGVsdm95ZQ1MaWNrIDMsIDIwMDANY2liYWNocmltZSBwcmludCBvbiBhbHVtaW51
bSwgZWQuICMgNS82DTQ5IDEvNCB4IDM5IDMvOCBpbmNoZXMgKDEyNSB4IDEwMCBjbSkNU1cgMDIy
Xc7Hr007KxuAoAKooQH3Mzb3E0BB2PrqUtIDbGVoDWlpqbSRQ1bqRv8ADUo+5DSkNRbqWgAKarQi
gFN9TTSyszuzWipIUEncVAAFDpyWJYmteoBt9NdSfiT6/wDiv6j1p6/H01weeJCpwuY4/LG42jiy
qmJhw2Lcy0ISgApvryGRXEOZ5pzWL49jBVKF8JZmn5Ht2C4pNEhFT7WWvqSNTKSql2kNLgTT2qoF
RvUL/DRivIvaRTTrU3AEkbGgOoXuANQH+Br8Nq9f8TqH3VqQBUmlRQj5HbUQY1rYCelq2hmB9N/+
mkdbhcLWIYMChUKCAFtKgDau2/pp2UOKmoIIJFA
------=_NextPart_000_002F_01C3F989.5E1B7780--
open(FILE, "/var/errorlog.txt");
$content=<FILE>;
close(FILE);
open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL <<"EOF";
From: xyz\@host.com
To: xyz\@host.com
Subject: Some subject
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_NextPart_000_002F_01C3F989.5E1B7780"
------=_NextPart_001_0030_01C3F989.5E1B7780
Content-Type: text/plain;
Content-Transfer-Encoding: quoted-printable
This is the message text
below is the binary content of a jpg in a new multipart section
------=_NextPart_000_002F_01C3F989.5E1B7780
Content-Type: text/plain; name="errorlog.txt"
Content-Disposition: attachment; filename="errorlog.txt"
Content-Transfer-Encoding: base64
$content
------=_NextPart_000_002F_01C3F989.5E1B7780--
EOF
close(MAIL);
I would suggest you use MIME::Lite (found on CPAN) - it's really easy and straight forward. Below is the sub I use to send a contact card. (I use qmail as a mail server - but it should make sense.)
use strict;
use MIME::Lite;
sub sendEmail {
my $name = $_[0];
my $from = $_[1];
my $to = $_[2];
my $subject = $_[3];
my $body = "Please see the attached.";
my $injector = "/var/qmail/bin/qmail-inject";
open (MESSAGE, "| $injector -f '$from' '$to'") or erp("> Can't open $injector!\n");
my $msg;
$msg = MIME::Lite -> new(
From => "$name <$from>",
To => "$to",
Subject => "$subject",
Type => "multipart/mixed"
);
$msg -> attach(
Type => "text/html",
Data => "$body"
);
Okay here is the script I used, basically the script you had:
Code:
#!/opt/perl/bin/perl -w
use MIME::Lite;
use Getopt::Std;
use Net::SMTP;
my $SMTP_SERVER = '/usr/sbin/sendmail -t';
my $DEFAULT_SENDER = 'dford@tacsea.metapath.com';
my $DEFAULT_RECIPIENT = 'don.ford@metapath.com';
my (%o, $msg);
# process options
getopts('hf:t:s:', \%o);
$o{f} ||= $DEFAULT_SENDER;
$o{t} ||= $DEFAULT_RECIPIENT;
$o{s} ||= 'attachment';
if ($o{h} or !@ARGV) {
die "usage:\n\t$0 -h -f -t -s /var/log/log.txt\n";
}
# construct and send email
$msg = new MIME::Lite(
From => $o{f},
To => $o{t},
Subject => $o{s},
Data => "Hi",
Type => "multipart/mixed",
);
while (@ARGV) {
$msg->attach('Type' => 'application/octet-stream',
'Encoding' => 'base64',
'Path' => shift @ARGV);
}
$msg->send;
I did not see the "Hi", but otherwise this worked fine. (I did change the email addresses to reduce spam potential.) I did have some issues with a bad To email address at first, but you should see the bounce messages in your mail box on the machine. I wonder if you are having issues with sendmail?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.