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 |
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 |
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 |
Code:
open(FILE, "/var/errorlog.txt"); |
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 |
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 |
but this way from david_ross should work also
and I want to know what's wrong. regards cccc |
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); } |
Quote:
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? :cool: |
hi dford
and where should I put the path to sendmail on your script ? kind regards cccc |
My script was just a quick hack test. Where I had:
Code:
open(MAIL, "| cat > foo.txt"); Code:
open(MAIL, "|/usr/sbin/sendmail -t"); Code:
close(MAIL); |
hi
I have a script , NO ERRORS, but NO MAILS ! knows someone what's wrong ? Code:
#!/usr/bin/perl -w cccc |
It looks like you just need to add
Code:
$msg->send(); |
hi dford
I've added. but still NO mails ! kind regards cccc |
Okay here is the script I used, basically the script you had:
Code:
#!/opt/perl/bin/perl -w |
thanks dford
I don't know, what's wrong. I use the same script and it doesn't send any mails ! I've spend a lot of time about this problem. greetings cccc |
Is sendmail running? I tested sending through sendmail directly with this:
Code:
echo 'From: dford1234@metapath.com Note: email addresses were altered to not add to my spam load. You can probably figure them out though. :D |
Yes !
this one for example works excellent and sends mail: Code:
open(MAIL, "|/usr/sbin/sendmail -t"); and the path to postfix is: /usr/sbin/sendmail I use this path at all other scripts and it works. kind regards cccc |
Hmmm...try something like this instead of the $msg->send(); line:
Code:
$str = $msg->as_string; |
I've replaced, but now I have on this new lines errors:
[Fri Mar 5 22:07:47 2004] mail.cgi: Global symbol "$str" requires explicit package name at mail.cgi line 58. [Fri Mar 5 22:07:47 2004] mail.cgi: Global symbol "$str" requires explicit package name at mail.cgi line 60. [Fri Mar 5 22:07:47 2004] mail.cgi: Execution of mail.cgi aborted due to compilation errors. |
You might just add:
my $str; |
thanks for your time and help, but still no mails
|
So, just to be clear, when you enter:
Code:
perl mail.cgi /var/log/log.txt |
Hi
sorry, my mistake the file was not readable. now it works ! thanks for your time and HELP ! cccc |
You are welcome! Glad it is working now!
|
All times are GMT -5. The time now is 12:53 AM. |