[ Index ]
 

Code source de Dolibarr 2.0.1

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/scripts/ -> newsletter-send.pl (source)

   1  #!/usr/bin/perl
   2  
   3  # Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
   4  #
   5  # This program is free software; you can redistribute it and/or modify
   6  # it under the terms of the GNU General Public License as published by
   7  # the Free Software Foundation; either version 2 of the License, or
   8  # (at your option) any later version.
   9  #
  10  # This program is distributed in the hope that it will be useful,
  11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  # GNU General Public License for more details.
  14  #
  15  # You should have received a copy of the GNU General Public License
  16  # along with this program; if not, write to the Free Software
  17  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18  #
  19  # $Id: newsletter-send.pl,v 1.2 2003/03/25 21:47:08 rodolphe Exp $
  20  # $Source: /cvsroot/dolibarr/dolibarr/scripts/newsletter-send.pl,v $
  21  
  22  $SYSLOG_LEVEL = 'local3';
  23  
  24  use strict;
  25  use vars qw($SYSLOG_LEVEL);
  26  use DBI;
  27  use Net::SMTP;
  28  use Text::Wrap;
  29  use Getopt::Long;
  30  use Sys::Syslog qw(:DEFAULT setlogsock);
  31   Getopt::Long::Configure("bundling");
  32  
  33  my($debug,$verbose, $help) = (0,0,0);
  34  
  35  exit unless GetOptions("v+", \$verbose, "debug", \$debug, "help", \$help);
  36  
  37  print_help() if $help;
  38  
  39  unless (defined $ENV{"DBI_DSN"}) {
  40      print "Missing ENV var: DBI_DSN is not defined\n";
  41      exit 0;
  42  }
  43  
  44  
  45  my($dbh, $sth, $sthi, $i, $sqli, $sql, $stha, $digest);
  46  
  47  print "Running in verbose mode level $verbose\n" if $verbose>0;
  48  
  49  my $sl = Sys::Syslog::setlogsock('unix');
  50  $sl = Sys::Syslog::openlog('send-newsletter.pl', 'pid', $SYSLOG_LEVEL);
  51  $sl = Sys::Syslog::syslog('info', 'Start');
  52  
  53  print "Start\n"  if $verbose>0;
  54  
  55  print "DBI connection : open\n" if $verbose>3;
  56  $dbh = DBI->connect() || die $DBI::errstr;
  57  
  58  #
  59  #
  60  # Lecture des infos de la base
  61  #
  62  #
  63  #   email_subject      varchar(32) NOT NULL,
  64  #   email_from_name    varchar(255) NOT NULL,
  65  #   email_from_email   varchar(255) NOT NULL,
  66  #   email_replyto      varchar(255) NOT NULL,
  67  #   email_body         text,
  68  #   target             smallint,
  69  #   sql_target         text,
  70  #   status             smallint NOT NULL DEFAULT 0,
  71  #   date_send_request  datetime,   -- debut de l'envoi demandé
  72  #   date_send_begin    datetime,   -- debut de l'envoi
  73  #   date_send_end      datetime,   -- fin de l'envoi
  74  #   nbsent             integer,    -- nombre de mails envoyés
  75  
  76  my $sqli = "SELECT rowid, email_subject, email_from_name, email_from_email, email_replyto, email_body, target, sql_target, status, date_send_request, date_send_begin, date_send_end, nbsent";
  77  
  78  $sqli .= " FROM llx_newsletter WHERE status=2 AND date_send_request < now()";
  79  $sthi = $dbh->prepare($sqli);
  80  
  81  $sthi->execute;
  82  
  83  my ($hsri);
  84  while ( $hsri = $sthi->fetchrow_hashref ) {
  85  
  86      #
  87      # Update newsletter
  88      #
  89      if (!$debug) {
  90      $stha = $dbh->prepare("UPDATE llx_newsletter SET status=4,date_send_begin=now() WHERE rowid=" . $hsri->{"rowid"});
  91      $stha->execute;
  92      $stha->finish;
  93      }
  94  
  95      #
  96      #
  97      #
  98      my ($fromemail, $from, $replyto, $subject, $mesg);
  99  
 100      $from      = $hsri->{"email_from_name"} . " <" . $hsri->{"email_from_email"} . ">";
 101      $fromemail = $hsri->{"email_from_email"};
 102      $replyto   = $hsri->{"email_replyto"};
 103      $mesg      = $hsri->{"email_body"};
 104      $subject   = $hsri->{"email_subject"};
 105      $sql       = $hsri->{"sql_target"};
 106  
 107      print "Message de : $from\n" if $verbose;
 108  
 109      #
 110      # Read dest
 111      #
 112  
 113      if ($sql) {
 114  
 115      $sth = $dbh->prepare($sql);
 116      $sth->execute;
 117  
 118      my($nbdest, $nberror) = (0,0);
 119      
 120      while (my $hsr = $sth->fetchrow_hashref )
 121      {
 122  
 123          if (length($hsr->{"email"}) > 0)
 124          {
 125          my $firstname = $hsr->{"firstname"};
 126          my $lastname = $hsr->{"name"};
 127          my $email = "$firstname $lastname <".$hsr->{"email"}.">";
 128          
 129  
 130          if (!$debug)
 131          {
 132  
 133              if (! mail_it($hsr->{"email"},
 134                    $email,
 135                    $fromemail, 
 136                    $from,
 137                    $subject,
 138                    $mesg,
 139                    $replyto))
 140              {
 141              $nberror++;
 142              print $nberror;
 143  
 144              }
 145              
 146          }
 147          else
 148          {
 149              print "$nbdest : Mail $from -> ".$email."\n" if $verbose;
 150          }
 151          }
 152          
 153          $nbdest++;        
 154      }
 155  
 156      $sth->finish;
 157  
 158      #
 159      # Update newsletter
 160      #
 161      if (!$debug)
 162      {
 163          $stha = $dbh->prepare("UPDATE llx_newsletter SET status=3,date_send_end=now(), nbsent=$nbdest, nberror=$nberror WHERE rowid=" . $hsri->{"rowid"});
 164          $stha->execute;
 165          $stha->finish;
 166      }
 167      } else {
 168      print "No sql request";
 169      }
 170  
 171  }
 172  $sthi->finish;
 173  
 174  print "DBI connection : close\n" if $verbose>3;
 175  
 176  $dbh->disconnect;
 177  
 178  print "End\n" if $verbose>0;
 179  #
 180  # 
 181  #
 182      
 183      
 184  $sl = Sys::Syslog::syslog('info', 'End');
 185  
 186  Sys::Syslog::closelog();
 187  
 188  #
 189  #
 190  #
 191  #
 192  #
 193  sub print_help {
 194      print "Usage send-newsletter.pl [-v]\n";
 195      exit 0;
 196  }
 197  
 198  sub mail_it {
 199      my ($toemail, $to, $fromemail, $from, $subject, $mesg, $replyto) = @_;
 200      my ($smtp);
 201  
 202      $mesg = wrap("","",$mesg);
 203  
 204      $smtp = Net::SMTP->new('localhost',
 205                 Hello => 'localhost',
 206                 Timeout => 30);
 207      
 208      if ($smtp) {
 209  
 210      print "Mail $from -> ".$to."\n" if $verbose;
 211  
 212      if ($smtp->verify($toemail)) {
 213  
 214          $smtp->mail($fromemail);
 215          $smtp->to($toemail);
 216      
 217          $smtp->data();
 218          $smtp->datasend("From: $from\n");
 219          $smtp->datasend("Reply-To: $replyto\n") if $replyto;
 220          $smtp->datasend("Content-Type: text/plain; charset=\"iso-8859-1\"\n");
 221          $smtp->datasend("To: $to\n");
 222          $smtp->datasend("Subject: $subject\n");
 223          $smtp->datasend("X-Mailer: Dolibarr\n");
 224          $smtp->datasend("\n");
 225      
 226          $smtp->datasend($mesg);
 227          
 228          $smtp->dataend();
 229  
 230          return 1;
 231  
 232      } else {
 233          return 0;
 234      }
 235      
 236      $smtp->quit;
 237  
 238      } else {
 239      return 0;
 240      }
 241  }
 242  
 243  
 244  __END__
 245  # Below is the documentation for the script.
 246  
 247  =head1 NAME
 248  
 249  send-newsletter.pl - 
 250  
 251  =head1 SYNOPSIS
 252  
 253  send-newsletter.pl [-v]
 254  
 255  =head1 DESCRIPTION
 256  
 257  send-newsletter.pl send newsletter from DB
 258  
 259  =head1 OPTIONS
 260  
 261  =over
 262  
 263  =back
 264  
 265  =head1 AUTHOR
 266  
 267  Rodolphe Quiedeville (rodolphe@quiedeville.org)
 268  
 269  =cut
 270  


Généré le : Mon Nov 26 12:29:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics