GlisWeb framework
_mail.tools.php
Vai alla documentazione di questo file.
1 <?php
2 
34  function sendMail( $host, $from, $to, $oggetto, $corpo, $cc = array(), $bcc = array(), $attach = array(), $headers = array(), $user = NULL, $pasw = NULL, $port = 25 ) {
35 
36  // log
37  logWrite(
38  'sending: ' . $oggetto . ' ' .
39  'to: ' . print_r( $to , true ) . ' ' .
40  'cc: ' . print_r( $cc , true ) . ' ' .
41  'bcc: ' . print_r( $bcc , true ) . ' ' .
42  'attach: ' . print_r( $attach , true ) . ' ',
43  'mail',
44  LOG_DEBUG
45  );
46 
47  // esito dell'operazione
48  $status = true;
49 
50  // creazione dell'oggetto mail
51  $mail = new PHPMailer();
52 
53  // configurazione dell'oggetto mail
54  $mail->IsSMTP();
55  $mail->Host = $host;
56  $mail->Port = $port;
57  $mail->SMTPDebug = SMTP::DEBUG_SERVER;
58  $mail->Debugoutput = function( $str, $level ) { logWrite( '('.$level.') '.$str, 'phpmailer', LOG_DEBUG ); };
59 
60  // log
61  logWrite(
62  'server: ' . $host . ' ' .
63  'port: ' . $port . ' ' .
64  'user: ' . $user . ' ' .
65  'pass: ' . $pass ,
66  'mail',
67  LOG_DEBUG
68  );
69 
70  // autenticazione
71  if( ! empty( $user ) ) {
72  $mail->SMTPAuth = true;
73  $mail->Username = $user;
74  $mail->Password = $pasw;
75  } else {
76  $mail->SMTPAuth = false;
77  }
78 
79  // configurazione dell'oggetto mail
80  $mail->IsHTML = true;
81  $mail->CharSet = 'UTF-8';
82 
83  // mittente
84  $mail->SetFrom( current( $from ), current( array_keys( $from ) ) );
85  $mail->AddReplyTo( current( $from ), current( array_keys( $from ) ) );
86 
87  // oggetto
88  $mail->Subject = $oggetto;
89 
90  // creo il testo in plain text
91  $text = new \Html2Text\Html2Text( $corpo );
92 
93  // corpo alternativo
94  $mail->AltBody = wordwrap( $text->getText() );
95 
96  // corpo del messaggio
97  $mail->MsgHTML( $corpo );
98 
99  // destinatari
100  foreach( $to as $destName => $destAddress ) {
101  $mail->AddAddress( trim( $destAddress ), trim( $destName ) );
102  }
103 
104  // destinatari CC
105  foreach( $cc as $destName => $destAddress ) {
106  $mail->AddCC( trim( $destAddress ), trim( $destName ) );
107  }
108 
109  // destinatari BCC
110  foreach( $bcc as $destName => $destAddress ) {
111  $mail->AddBCC( trim( $destAddress ), trim( $destName ) );
112  }
113 
114  // allegati
115  foreach( $attach as $kAtch => $vAtch ) {
116  fullPath( $vAtch );
117  if( file_exists( $vAtch ) && is_readable( $vAtch ) ) {
118  $mail->AddAttachment( $vAtch , basename( $vAtch ) );
119  } else {
120  logWrite( 'impossibile allegare ' . $vAtch . ' (file non trovato o non leggibile)', 'mail', LOG_CRIT );
121  }
122  }
123 
124  // invio
125  $status = $mail->Send();
126 
127  // log
128  if( $status == false ) {
129  logWrite( 'errore phpmailer, status: ' . $status . ' ' . $mail->ErrorInfo . ' sending: '.$oggetto.' via: ' . $host . ':' . $port . ' to: '.serialize( $to ), 'mail', LOG_CRIT );
130  } else {
131  logWrite( 'messaggio inviato con successo, phpmailer status: ' . $status . ' sending: '.$oggetto.' to: '.serialize( $to ), 'mail', LOG_NOTICE );
132  }
133 
134  // restituzione risultato
135  return $status;
136 
137  }
138 
147  function queueMailFromTemplate( $c, $t, $d, $timestamp_invio, $to, $l = 'it-IT', $to_cc = array(), $to_bcc = array(), $headers = array(), $server = NULL ) {
148 
149  // debug
150  logWrite( 'richiesto accodamento di una mail con template', 'mail', LOG_DEBUG );
151 
152  // valuto il template manager
153  switch( $t['type'] ) {
154 
155  case 'twig':
156 //print_r( $t );
157 //print_r( $d['ct'] );
158 
159  // avvio di Twig
160  $twig = new Twig_Environment( new Twig_Loader_Array( $t[ $l ] ) );
161  $from = new Twig_Environment( new Twig_Loader_Array( array( 'nome' => array_key_first( $t[ $l ]['from'] ), 'mail' => reset( $t[ $l ]['from'] ) ) ) );
162 # $to = new Twig_Environment( new Twig_Loader_Array( array( 'nome' => array_key_first( $t[ $l ]['to'] ), 'mail' => reset( $t[ $l ]['to'] ) ) ) );
163 
164  // variabili da passare a queueMail()
165  $mittente = array( $from->render( 'nome', $d ) => $from->render( 'mail', $d ) );
166  $oggetto = $twig->render( 'oggetto', $d );
167  $corpo = $twig->render( 'testo', $d );
168  $allegati = ( ( isset($t[ $l ]['attach'] ) ) ? $t[ $l ]['attach'] : array() );
169 #print_r($corpo );
170  // se è definito nel template imposto il destinatario
171  if( array_key_exists('to', $t[ $l ] ) && is_array( $t[ $l ]['to'] ) && ! empty( $t[ $l ]['to'][ array_key_first($t[ $l ]['to'] ) ] ) ) {
172 #print_r( $t[$l] );
173  //$to = array_replace_recursive( $to, $t[ $l ]['to'] );
174 #print_r( $to );
175  $destinatari[ array_key_first($t[ $l ]['to'] ) ] = $t[ $l ]['to'][ array_key_first( $t[ $l ]['to'] ) ];
176  }
177 
178  // elaboro i placeholder nei destinatari
179  if( isset( $to ) ){
180  foreach( $to as $k => $v ) {
181 
182  $tm = array( 'nome' => $k, 'mail' => $v );
183 
184  $tw = new Twig_Environment( new Twig_Loader_Array( $tm ) );
185 
186  $destinatari[ $tw->render( 'nome', $d ) ] = $tw->render( 'mail', $d );
187  }
188  }
189 
190  // TODO
191  $destinatari_cc = $to_cc;
192  $destinatari_bcc = $to_bcc;
193 
194  break;
195 
196  default:
197 
198  // debug
199  logWrite( 'tipo di template non supportato: ' . $t['type'], 'mail', LOG_ERR );
200 
201  break;
202 
203  }
204 
205  // accodo la mail
206  $id = queueMail(
207  $c,
209  $mittente,
210  $destinatari,
211  $oggetto,
212  $corpo,
213  $destinatari_cc,
214  $destinatari_bcc,
215  $allegati,
216  $headers,
217  $server
218  );
219 
220  // debug
221  logWrite( 'mail accodata via template con id #' . $id, 'mail', LOG_ERR );
222 
223  // ritorno
224  return $id;
225 
226  }
227 
236  function queueMail( $c, $timestamp_invio, $mittente, $destinatari, $oggetto, $corpo, $destinatari_cc = array(), $destinatari_bcc = array(), $allegati = array(), $headers = array(), $server = NULL ) {
237 
238  // lock delle tabelle della coda
239  $lock = mysqlQuery( $c, 'LOCK TABLES mail_out WRITE' );
240 
241  // se il lock è andato a buon fine
242  if( $lock === true ) {
243 
244  // inserimento della mail in coda
245  $id = mysqlQuery(
246  $c,
247  "INSERT INTO mail_out (
248  timestamp_composizione
249  ,
250  timestamp_invio
251  ,
252  server
253  ,
254  mittente
255  ,
256  destinatari
257  ,
258  destinatari_cc
259  ,
260  destinatari_bcc
261  ,
262  oggetto
263  ,
264  corpo
265  ,
266  allegati
267  ,
268  headers
269  ) VALUES (
270  ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
271  )",
272  array(
273  array( 's' => time() )
274  ,
275  array( 's' => $timestamp_invio )
276  ,
277  array( 's' => $server )
278  ,
279  array( 's' => serialize( $mittente ) )
280  ,
281  array( 's' => serialize( $destinatari ) )
282  ,
283  array( 's' => serialize( $destinatari_cc ) )
284  ,
285  array( 's' => serialize( $destinatari_bcc ) )
286  ,
287  array( 's' => $oggetto )
288  ,
289  array( 's' => $corpo )
290  ,
291  array( 's' => serialize( $allegati ) )
292  ,
293  array( 's' => serialize( $headers ) )
294  )
295  );
296 
297  // unlock delle tabelle
298  mysqlQuery( $c, 'UNLOCK TABLES' );
299 
300  // valore di ritorno
301  return $id;
302 
303  } else {
304 
305  // valore di ritorno
306  return false;
307 
308  }
309 
310  }
311 
312 ?>
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
Definition: _log.utils.php:48
mysqlQuery( $c, $q, $p=false, &$e=array())
if(!empty($_REQUEST['id'])) $d
queueMail( $c, $timestamp_invio, $mittente, $destinatari, $oggetto, $corpo, $destinatari_cc=array(), $destinatari_bcc=array(), $allegati=array(), $headers=array(), $server=NULL)
accoda una mail
sendMail( $host, $from, $to, $oggetto, $corpo, $cc=array(), $bcc=array(), $attach=array(), $headers=array(), $user=NULL, $pasw=NULL, $port=25)
invia una mail
Definition: _mail.tools.php:34
$destinatari
Definition: _template01.php:43
$timestamp_invio
Definition: _template01.php:48
queueMailFromTemplate( $c, $t, $d, $timestamp_invio, $to, $l='it-IT', $to_cc=array(), $to_bcc=array(), $headers=array(), $server=NULL)
accoda una mail utilizzando un template
fullPath(&$f)