LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2004, 06:52 PM   #1
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Rep: Reputation: 51
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";


Attachment ?


close(MAIL);
}


kind regards
cccc
 
Old 02-25-2004, 01:12 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
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--
 
Old 02-25-2004, 04:11 PM   #3
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
thanks

but how will be the perl code, if I can ask you
to read to read the source from /var/errorlog.txt ?

regards
cccc
 
Old 02-26-2004, 12:31 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Code:
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);
 
Old 02-26-2004, 01:55 PM   #5
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
hallo david_ross

I've tried this but wont work:

open(FILE, "/var/errorlog.txt");
$content=<FILE>;
close(FILE);

open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL <<"EOF";
From:root\@domain.net
To:xxx\@domain.net
Subject: transfer was successfully !

hi,

transfer was successfully !

$content

Time: ", scalar localtime,

EOF

close(MAIL);


I get error mesage for 2 lines:

$content=<FILE>;

" Global symbol "$content" requires explicit package name "

and

$content

" Global symbol "$content" requires explicit package name "


regards
ccc
 
Old 02-26-2004, 04:08 PM   #6
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
At CPAN there is a module that does just what you need: Net-SMTP-Multipart-1.5.tar.gz

It relies on Net::SMTP and Mime::Base64. Be sure to read the POD it tells you how to use it for just what you asked for. The location is: http://www.cpan.org/authors/id/D/DR/...art-1.5.tar.gz
 
Old 02-26-2004, 05:44 PM   #7
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
but this way from david_ross should work also
and I want to know what's wrong.

regards
cccc
 
Old 02-26-2004, 05:56 PM   #8
hallamigo
Member
 
Registered: Feb 2004
Location: Utah, USA
Distribution: Debian
Posts: 230

Rep: Reputation: 31
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"
);

$msg -> attach(
Type => "text/x-vcard",
Path => "/usr/local/apache/vcard.vcf",
Filename => "info.vcf",
Disposition => "attachment"
);

$msg -> print(\*MESSAGE);
close (MESSAGE);
}
 
Old 02-26-2004, 09:50 PM   #9
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
Quote:
Originally posted by cccc
but this way from david_ross should work also
and I want to know what's wrong.

regards
cccc


Well basically it ought to work. Here is my test script:

#!/usr/bin/perl

$localtime = localtime();
open(FILE,"/var/log/dmesg");
$content = <FILE>;
open(MAIL, "| cat > foo.txt");

print MAIL << "EOF";
From: dford
To: dford
Subject: Successful

Hi!
$content

Time: $localtime

EOF
exit;


And my results are:

From: dford
To: dford
Subject: Successful

Hi!
Linux version 2.4.20-28.9 (bhcompile@porky.devel.redhat.com) (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #1 Thu Dec 18 13:45:22 EST 2003


Time: Thu Feb 26 21:36:20 2004


So, what else is in your script?
 
Old 02-29-2004, 06:57 PM   #10
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
hi dford

and where should I put the path to sendmail on your script ?

kind regards
cccc
 
Old 03-01-2004, 02:28 PM   #11
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
My script was just a quick hack test. Where I had:
Code:
open(MAIL, "| cat > foo.txt");
You should replace that with david_ross's:
Code:
open(MAIL, "|/usr/sbin/sendmail -t");
You also should probably add:
Code:
close(MAIL);
before the exit; as well.
 
Old 03-03-2004, 07:16 PM   #12
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
hi

I have a script , NO ERRORS, but NO MAILS !

knows someone what's wrong ?


Code:
#!/usr/bin/perl -w
use MIME::Lite;
use Getopt::Std;
use Net::SMTP;

my $SMTP_SERVER = '/usr/sbin/sendmail -t';
my $DEFAULT_SENDER = 'sender@domain.net';
my $DEFAULT_RECIPIENT = 'recipient@domain.net';

MIME::Lite->send('sendmail', $SMTP_SERVER, Timeout=>60);

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);
}
kind regards
cccc

Last edited by cccc; 03-04-2004 at 04:16 AM.
 
Old 03-03-2004, 08:32 PM   #13
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
It looks like you just need to add
Code:
$msg->send();
to the end.
 
Old 03-04-2004, 04:44 AM   #14
cccc
Senior Member
 
Registered: Sep 2003
Distribution: Debian Squeeze / Wheezy
Posts: 1,623

Original Poster
Rep: Reputation: 51
hi dford

I've added.
but still NO mails !

kind regards
cccc
 
Old 03-04-2004, 09:58 AM   #15
dford
Member
 
Registered: May 2003
Location: Kansas
Distribution: RH 9, OpenBSD, FreeBSD
Posts: 60

Rep: Reputation: 19
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl send mail code pragti Programming 2 03-17-2006 05:48 PM
Send Mail with Perl Cool_Hand_Luke Programming 2 03-14-2005 09:59 AM
How to send email with attachment using "mail"? jonathanztaub Linux - Software 4 05-10-2004 11:53 AM
I'm looking for a simply perl script to send mail, when ping down cccc Programming 2 01-12-2004 04:36 AM
mail -a would not work to send e-mail attachment saavik Linux - Networking 3 12-18-2003 09:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration