Re: [lms] lms-sendinvoices - wysyłanie przez zewnętrzny serwer SMTP
Dawid Widyna napisał(a):
Kiedy przepiszę "na świeżo" skrypt wyślę na grupę - może przyda się komuś z podobnymi problemami.
Witam,
w załączniku znajduje się skrypcik. Wszelkie uwagi mile widziane. Zwróćcie uwagę na początek skryptu - w lms.ini należy zdefiniować kilka dodatkowych parametrów...
Wersja lms: 1.8.13 (jak wreszcie mi się uda zrobić upgrejd do 1.10, na pewno przetestuję).
pozdrawiam, widynek
#!/usr/bin/perl # # LMS version 1.8.13 Tessa # # Copyright (C) 2001-2006 LMS Developers # # Please, see the doc/AUTHORS for more information about authors! # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License Version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, # USA. # # $Id: lms-sendinvoices,v 1.28.2.2 2006/01/16 09:35:17 alec Exp $
use strict; use DBI; use Config::IniFiles; use Getopt::Long; use vars qw($configfile $help $version $quiet $fakedate); use POSIX qw(strftime); use LWP::UserAgent; use Time::Local; use MIME::QuotedPrint; use Text::Iconv; use Mail::SendEasy;
my $_version = '1.8.13 Tessa';
my %options = ( "--config-file|C=s" => $configfile, "--quiet|q" => $quiet, "--help|h" => $help, "--version|v" => $version, "--fakedate|f=s" => $fakedate, );
Getopt::Long::config("no_ignore_case"); GetOptions(%options);
if($help) { print STDERR <<EOF; lms-sendinvoices, version $_version (C) 2001-2006 LMS Developers
-C, --config-file=/etc/lms/lms.ini alternate config file (default: /etc/lms/lms.ini); -q, --quiet suppress any output, except errors; -h, --help print this help and exit; -v, --version print version info and exit; -f, --fakedate=YYYY/MM/DD override system date;
EOF exit 0; }
if($version) { print STDERR <<EOF; lms-sendinvoices, version $_version (C) 2001-2006 LMS Developers
EOF exit 0; }
if(!$configfile) { $configfile = "/etc/lms/lms.ini"; }
if(! -r $configfile) { print STDERR "Fatal error: Unable to read configuration file $configfile, exiting.\n"; exit 1; }
if(!$quiet) { print STDOUT "lms-sendinvoices, version $_version\n"; print STDOUT "(C) 2001-2006 LMS Developers\n"; print STDOUT "Using file $configfile as config.\n"; }
my $ini = new Config::IniFiles -file => $configfile; print @Config::IniFiles::errors;
my $dbtype = $ini->val('database', 'type') || 'mysql'; my $dbhost = $ini->val('database', 'host') || 'localhost'; my $dbuser = $ini->val('database', 'user') || 'root'; my $dbpasswd = $ini->val('database', 'password') || ''; my $dbname = $ini->val('database', 'database') || 'lms'; my $dbencoding = $ini->val('database', 'server_encoding') || 'UTF-8';
my $filetype = $ini->val('invoices', 'type') || '';
my $lms_url = $ini->val('sendinvoices', 'lms_url') || 'http://localhost/lms/'; my $lms_user = $ini->val('sendinvoices', 'lms_user') || ''; my $lms_password = $ini->val('sendinvoices', 'lms_password') || ''; my $debug_email = $ini->val('sendinvoices', 'debug_email') || ''; my $sender_name = $ini->val('sendinvoices', 'sender_name') || ''; my $sender_email = $ini->val('sendinvoices', 'sender_email') || ''; my $mail_subject = $ini->val('sendinvoices', 'mail_subject') || 'Invoice No. %invoice'; my $smtp_user = $ini->val('sendinvoices', 'smtp_user') || ''; my $smtp_pass = $ini->val('sendinvoices', 'smtp_pass') || ''; my $smtp_host = $ini->val('sendinvoices', 'smtp_host') || ''; #my $mail_body = $ini->val('sendinvoices', 'mail_body') || 'Attached file with Invoice No. %invoice';
my $dst_file = '/tmp/tmp/faktura.pdf';
if(!$sender_name) { print STDERR "Fatal error: sender_name unset! Can't continue, exiting.\n"; exit 1; }
if(!$sender_email) { print STDERR "Fatal error: sender_email unset! Can't continue, exiting.\n"; exit 1; }
if(!$smtp_host || !$smtp_user || !$smtp_pass) { print STDERR "Fatal error: I need params for SMTP connection! Can't continue, exiting.\n"; exit 1; }
my $dbase; my $utsfmt;
if($dbtype eq "mysql") { $dbase = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 }); $utsfmt = "UNIX_TIMESTAMP()"; } elsif($dbtype eq "postgres") { $dbase = DBI->connect("DBI:Pg:dbname=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 }); $utsfmt = "EXTRACT(EPOCH FROM CURRENT_TIMESTAMP(0))"; } else { print STDERR "Fatal error: unsupported database type: $dbtype, exiting.\n"; exit 1; }
#przestawiamy format faktur w bazie na PDF - w biurze uzywamy HTML'a my $dbq = $dbase->prepare("UPDATE uiconfig SET value='standard' WHERE section='invoices' AND var='template_file'"); $dbq->execute(); $dbq = $dbase->prepare("UPDATE uiconfig SET value='pdf' WHERE section='invoices' AND var='type'"); $dbq->execute();
$dbq = $dbase->prepare("SELECT value FROM uiconfig WHERE section='invoices' AND var='type' AND disabled=0"); $dbq->execute(); if(my $row = $dbq->fetchrow_hashref()) { $filetype = $row->{'value'}; }
my $fencoding = 'quoted-printable'; my $ftype = 'text/html'; my $fext = 'html';
if($filetype eq 'pdf') { my $ftype = 'application/octetstream'; my $fencoding = ''; my $fext = 'pdf'; }
sub localtime2() { if($fakedate) { my @fakedate = split(///, $fakedate); return localtime(timelocal(0,0,0,$fakedate[2],$fakedate[1]-1,$fakedate[0])); } else { return localtime(); } }
my $converter = Text::Iconv->new($dbencoding, "UTF-8");
$sender_name = '=?UTF-8?Q?'.encode_qp($sender_name, '').'?=';
my $month = sprintf("%d",strftime("%m",localtime2())); my $day = strftime("%e",localtime2()); my $year = strftime("%Y",localtime2()); my $daystart = strftime("%s", 0, 0, 0, $day, $month - 1, $year - 1900); my $dayend = strftime("%s", 59, 59, 23, $day, $month - 1, $year - 1900);
$dbq = $dbase->prepare('SELECT documents.id AS id, number, cdate, email, documents.name AS name, customerid, template FROM documents LEFT JOIN customers ON customers.id = customerid LEFT JOIN numberplans ON numberplanid = numberplans.id WHERE deleted = 0 AND type = 1 AND email != '' AND cdate >= '.$daystart.' AND cdate <= '.$dayend); $dbq->execute(); while(my $row = $dbq->fetchrow_hashref()) { my $ua = LWP::UserAgent->new; $ua->timeout(240); my $response = $ua->get($lms_url.'/?m=invoice&fetchsingle=1&override=1&id='.$row->{'id'}.'&loginform[login]='.$lms_user.'&loginform[pwd]='.$lms_password);
my $custemail = $debug_email || $row->{'email'}; my $invoice_number = $row->{'template'} || '%N/LMS/%Y'; $invoice_number =~ s/%(\d*)N/sprintf"%0${1}d",$row->{'number'}/e; $invoice_number = strftime($invoice_number, localtime($row->{'cdate'}));
my $subject = $mail_subject; $subject =~ s/%invoice/$invoice_number/g; my $msgid = $row->{'id'}; #zapisujemy fakturkê do pliku open(DSTFILE, ">$dst_file") or die("nie udalo sie otworzyc pliku $dst_file do zapisu"); print DSTFILE $response->content; close (DSTFILE); chown 0,0, $dst_file; chmod 0700, $dst_file;
my $status = Mail::SendEasy::send( smtp => $smtp_host , user => $smtp_user , pass => $smtp_pass , from => $sender_email , from_title => $sender_name , reply => $sender_email , error => $sender_email , to => $custemail , subject => '=?UTF-8?Q?'.encode_qp($subject, '').'?=', anex => $dst_file , msg => "Dzieñ dobry,\n\nw za³±czniku do tej wiadomo¶ci znajduje siê elektroniczny obraz faktury VAT.\n\nDo obejrzenia faktury konieczny jest czytnik PDF - np. program Foxit Reader lub Adobe Acrobat Reader.\n\nZ góry dziêkujemy za terminowe uregulowanie p³atno¶ci.\n\n--\npozdrawiamy,\nMy-wielcy ISP ;)" , # html => "<b>Tutaj mo¿na wkleiæ co¶ w HTML'u</b>" , msgid => $msgid , ) ;
if ($status) { print STDOUT "Wyslano mail do $custemail\n"; } if (!$status) { Mail::SendEasy::error ;} }
##ustawiamy format faktur z powrotem na HTML $dbq = $dbase->prepare("UPDATE uiconfig SET value='html' WHERE section='invoices' AND var='type'"); $dbq->execute(); $dbq = $dbase->prepare("UPDATE uiconfig SET value='invoice.html' WHERE section='invoices' AND var='template_file'"); $dbq->execute();
$dbq->finish(); $dbase->disconnect();
_______________________________________________ lms mailing list lms@lists.lms.org.pl http://lists.lms.org.pl/mailman/listinfo/lms
uczestnicy (1)
-
Dawid Widyna