34 function sendMail( $host, $from, $to, $oggetto, $corpo, $cc = array(), $bcc = array(), $attach = array(),
$headers = array(), $user = NULL, $pasw = NULL, $port = 25 ) {
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 ) .
' ',
51 $mail =
new PHPMailer();
57 $mail->SMTPDebug = SMTP::DEBUG_SERVER;
58 $mail->Debugoutput =
function( $str, $level ) {
logWrite(
'('.$level.
') '.$str,
'phpmailer', LOG_DEBUG ); };
62 'server: ' . $host .
' ' .
63 'port: ' . $port .
' ' .
64 'user: ' . $user .
' ' .
71 if( ! empty( $user ) ) {
72 $mail->SMTPAuth =
true;
73 $mail->Username = $user;
74 $mail->Password = $pasw;
76 $mail->SMTPAuth =
false;
81 $mail->CharSet =
'UTF-8';
84 $mail->SetFrom( current( $from ), current( array_keys( $from ) ) );
85 $mail->AddReplyTo( current( $from ), current( array_keys( $from ) ) );
88 $mail->Subject = $oggetto;
91 $text = new \Html2Text\Html2Text( $corpo );
94 $mail->AltBody = wordwrap( $text->getText() );
97 $mail->MsgHTML( $corpo );
100 foreach( $to as $destName => $destAddress ) {
101 $mail->AddAddress( trim( $destAddress ), trim( $destName ) );
105 foreach( $cc as $destName => $destAddress ) {
106 $mail->AddCC( trim( $destAddress ), trim( $destName ) );
110 foreach( $bcc as $destName => $destAddress ) {
111 $mail->AddBCC( trim( $destAddress ), trim( $destName ) );
115 foreach( $attach as $kAtch => $vAtch ) {
117 if( file_exists( $vAtch ) && is_readable( $vAtch ) ) {
118 $mail->AddAttachment( $vAtch , basename( $vAtch ) );
120 logWrite(
'impossibile allegare ' . $vAtch .
' (file non trovato o non leggibile)',
'mail', LOG_CRIT );
129 logWrite(
'errore phpmailer, status: ' .
$status .
' ' . $mail->ErrorInfo .
' sending: '.$oggetto.
' via: ' . $host .
':' . $port .
' to: '.serialize( $to ),
'mail', LOG_CRIT );
131 logWrite(
'messaggio inviato con successo, phpmailer status: ' .
$status .
' sending: '.$oggetto.
' to: '.serialize( $to ),
'mail', LOG_NOTICE );
150 logWrite(
'richiesto accodamento di una mail con template',
'mail', LOG_DEBUG );
153 switch(
$t[
'type'] ) {
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'] ) ) ) ); 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() );
171 if( array_key_exists(
'to',
$t[ $l ] ) && is_array(
$t[ $l ][
'to'] ) && ! empty(
$t[ $l ][
'to'][ array_key_first(
$t[ $l ][
'to'] ) ] ) ) {
175 $destinatari[ array_key_first(
$t[ $l ][
'to'] ) ] =
$t[
$l ][
'to'][ array_key_first(
$t[ $l ][
'to'] ) ];
180 foreach( $to as
$k => $v ) {
182 $tm = array(
'nome' =>
$k,
'mail' => $v );
184 $tw =
new Twig_Environment(
new Twig_Loader_Array( $tm ) );
191 $destinatari_cc = $to_cc;
192 $destinatari_bcc = $to_bcc;
199 logWrite(
'tipo di template non supportato: ' .
$t[
'type'],
'mail', LOG_ERR );
221 logWrite(
'mail accodata via template con id #' .
$id,
'mail', LOG_ERR );
236 function queueMail(
$c,
$timestamp_invio, $mittente,
$destinatari, $oggetto, $corpo, $destinatari_cc = array(), $destinatari_bcc = array(), $allegati = array(),
$headers = array(), $server = NULL ) {
242 if(
$lock ===
true ) {
247 "INSERT INTO mail_out ( 248 timestamp_composizione 270 ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? 273 array(
's' => time() )
277 array(
's' => $server )
279 array(
's' => serialize( $mittente ) )
283 array(
's' => serialize( $destinatari_cc ) )
285 array(
's' => serialize( $destinatari_bcc ) )
287 array(
's' => $oggetto )
289 array(
's' => $corpo )
291 array(
's' => serialize( $allegati ) )
293 array(
's' => serialize(
$headers ) )
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
if(!empty($_REQUEST['id'])) $d