pluginGino-phpmailer
 All Data Structures Namespaces Files Functions Variables Pages
plugin.phpmailer.php
Go to the documentation of this file.
1 <?php
196 namespace Gino\Plugin;
197 
198 require_once(LIB_DIR.OS.'PHPMailer/class.phpmailer.php');
199 require_once(LIB_DIR.OS.'PHPMailer/class.pop3.php');
200 require_once(LIB_DIR.OS.'PHPMailer/class.smtp.php');
201 
210 
214  private $_view_folder;
215 
220 
226  function __construct() {
227 
228  $this->_dft_view_folder = VIEWS_DIR;
229  }
230 
238  private function setSmtpParams($name) {
239 
240  $params = array();
241  if($name == 'gmail')
242  {
243  $params['smtp_auth'] = true;
244  $params['smtp_secure'] = 'tls';
245  $params['smtp_server'] = "smtp.gmail.com";
246  $params['smtp_port'] = 587;
247  }
248  return $params;
249  }
250 
280  private function sendPhpMail($to, $subject, $message, $options=array()) {
281 
282  $sender_email = array_key_exists('sender_email', $options) ? $options['sender_email'] : null;
283  $sender_name = array_key_exists('sender_name', $options) ? $options['sender_name'] : null;
284  $reply_email = array_key_exists('reply_email', $options) ? $options['reply_email'] : null;
285  $reply_name = array_key_exists('reply_name', $options) ? $options['reply_name'] : null;
286  $cc = array_key_exists('cc', $options) ? $options['cc'] : null;
287  $view_mailer = array_key_exists('view_mailer', $options) ? $options['view_mailer'] : false;
288  $charset = array_key_exists('charset', $options) ? $options['charset'] : 'UTF-8';
289  $crlf = array_key_exists('crlf', $options) && $options['crlf'] ? $options['crlf'] : "\r\n";
290 
291  $additional_headers = null;
292  $additional_parameters = null;
293 
294  if($sender_email || $reply_email || $view_mailer || $cc)
295  {
296  $additional_headers = '';
297 
298  if($sender_email)
299  {
300  $additional_headers .= "From:";
301  if($sender_name) $additional_headers .= $sender_name." <";
302  $additional_headers .= $sender_email;
303  if($sender_name) $additional_headers .= ">";
304 
305  if($reply_email || $view_mailer || $cc)
306  $additional_headers .= $crlf;
307  }
308  if($reply_email)
309  {
310  $additional_headers .= "Reply-To:";
311  if($reply_name) $additional_headers .= $reply_name." <";
312  $additional_headers .= $reply_email;
313  if($reply_name) $additional_headers .= ">";
314 
315  if($view_mailer || $cc)
316  $additional_headers .= $crlf;
317  }
318  if($view_mailer)
319  {
320  $additional_headers .= "X-Mailer: PHP/" . phpversion();
321  if($cc)
322  $additional_headers .= $crlf;
323  }
324  if($cc)
325  {
326  $additional_headers .= "Cc:".$cc;
327  }
328  }
329 
330  if($charset == 'UTF-8')
331  {
332  $header_charset = 'MIME-Version: 1.0'."\r\n".'Content-type: text/plain; charset=UTF-8'.$crlf;
333  $subject = "=?UTF-8?B?".base64_encode($subject).'?=';
334  $additional_headers = $header_charset.$additional_headers;
335  }
336 
337  $send = \mail($to, $subject, $message, $additional_headers, $additional_parameters);
338 
339  return $send;
340  }
341 
403  public function sendMail($recipient_email, $sender_email, $subject, $contents, $options=array()) {
404 
405  $php_mail = array_key_exists('php_mail', $options) ? $options['php_mail'] : true;
406  $mailer = array_key_exists('mailer', $options) ? $options['mailer'] : 'mail';
407 
408  // Intestazioni
409  $sender_name = array_key_exists('sender_name', $options) ? $options['sender_name'] : '';
410  $reply_email = array_key_exists('reply_email', $options) ? $options['reply_email'] : '';
411  $reply_name = array_key_exists('reply_name', $options) ? $options['reply_name'] : '';
412  $cc = array_key_exists('cc', $options) ? $options['cc'] : '';
413  $ccn = array_key_exists('ccn', $options) ? $options['ccn'] : '';
414  $notification = array_key_exists('notification', $options) ? $options['notification'] : '';
415 
416  // Allegati
417  $attachments = (array_key_exists('attachments', $options) && is_array($options['attachments'])) ? $options['attachments'] : array();
418  $embedded_images = (array_key_exists('embedded_images', $options) && is_array($options['embedded_images'])) ? $options['embedded_images'] : array();
419 
420  // Server smtp
421  $smtp_name = array_key_exists('smtp_name', $options) ? $options['smtp_name'] : null;
422  $smtp_secure = array_key_exists('smtp_secure', $options) ? $options['smtp_secure'] : null;
423  $smtp_server = array_key_exists('smtp_server', $options) ? $options['smtp_server'] : '';
424  $smtp_port = array_key_exists('smtp_port', $options) ? $options['smtp_port'] : 25;
425  $smtp_auth = array_key_exists('smtp_auth', $options) ? $options['smtp_auth'] : false;
426  $smtp_auth_type = array_key_exists('smtp_auth_type', $options) ? $options['smtp_auth_type'] : null;
427  $smtp_user = array_key_exists('smtp_user', $options) ? $options['smtp_user'] : '';
428  $smtp_password = array_key_exists('smtp_password', $options) ? $options['smtp_password'] : '';
429 
430  // Impostazioni Email
431  $exception = array_key_exists('exception', $options) ? $options['exception'] : false;
432  $debug = array_key_exists('debug', $options) ? $options['debug'] : 0;
433  $ishtml = array_key_exists('ishtml', $options) ? $options['ishtml'] : true;
434  $charset = array_key_exists('charset', $options) ? $options['charset'] : 'UTF-8';
435  $priority = array_key_exists('priority', $options) ? $options['priority'] : 3;
436  $view_mailer = array_key_exists('view_mailer', $options) ? $options['view_mailer'] : false;
437 
438  // Altro
439  $automatic_html = array_key_exists('automatic_html', $options) ? $options['automatic_html'] : false;
440  $alternative_text = array_key_exists('alternative_text', $options) ? $options['alternative_text'] : null;
441  $crlf = array_key_exists('crlf', $options) && $options['crlf'] ? $options['crlf'] : "\r\n";
442  $newline = array_key_exists('newline', $options) && $options['newline'] ? $options['newline'] : "\r\n";
443  $word_wrap = array_key_exists('word_wrap', $options) && $options['word_wrap'] ? $options['word_wrap'] : 50;
444 
445  if($php_mail)
446  {
447  return $this->sendPhpMail($recipient_email, $subject, $contenuto,
448  array(
449  'sender_email'=>$sender_email,
450  'sender_name'=>$sender_name,
451  'reply_email'=>$reply_email,
452  'reply_name'=>$reply_name,
453  'cc'=>$cc,
454  'charset'=>$charset,
455  'view_mailer'=>$view_mailer
456  )
457  );
458  }
459 
460  $mail = new \PHPMailer($exception);
461 
462  if($mailer == 'mail') {
463  $mail->isMail();
464  }
465  elseif($mailer == 'sendmail') {
466  $mail->isSendmail();
467  }
468  elseif($mailer == 'qmail') {
469  $mail->isQmail();
470  }
471  elseif($mailer == 'smtp') {
472  $mail->IsSMTP();
473  }
474  else {
475  return false;
476  }
477 
478  $mail->SMTPDebug = $debug;
479  $mail->Debugoutput = 'html';
480  $mail->Priority = $priority;
481  $mail->CharSet = $charset;
482 
483  // Set addresses
484  $mail->SetFrom($sender_email, $sender_name);
485  $mail->AddAddress($recipient_email);
486  if($reply_email) $mail->AddReplyTo($reply_email, $reply_name);
487 
488  if($cc) $mail->AddCC($cc);
489  if($ccn) $mail->AddBCC($ccn);
490  if($notification) $mail->ConfirmReadingTo = $notification;
491  // End
492 
493  if($smtp_name)
494  {
495  $params = $this->setSmtpParams($smtp_name);
496 
497  if(count($params))
498  {
499  if(array_key_exists('smtp_auth', $params)) $smtp_auth = $params['smtp_auth'];
500  if(array_key_exists('smtp_secure', $params)) $smtp_secure = $params['smtp_secure'];
501  if(array_key_exists('smtp_server', $params)) $smtp_server = $params['smtp_server'];
502  if(array_key_exists('smtp_port', $params)) $smtp_port = $params['smtp_port'];
503  }
504  }
505 
506  if($smtp_secure) $mail->SMTPSecure = $smtp_secure;
507  if($smtp_server) $mail->Host = $smtp_server;
508  if($smtp_port) $mail->Port = $smtp_port;
509 
510  if($smtp_auth)
511  {
512  $mail->SMTPAuth = $smtp_auth;
513  $mail->Username = $smtp_user;
514  $mail->Password = $smtp_password;
515 
516  if($smtp_auth_type) $mail->AuthType = $smtp_auth_type;
517  }
518 
519  // Invio Email
520  $mail->Subject = $subject;
521 
522  $mail->IsHTML($ishtml);
523  $mail->Body = $contents;
524  $mail->WordWrap = $word_wrap;
525 
526  if($ishtml) {
527 
528  if(!$alternative_text) {
529  $alternative_text = 'To view this email message, open it in a program that understands HTML!';
530  }
531  $mail->AltBody = $alternative_text;
532  }
533 
534  // Immagini inline
535  if(count($embedded_images))
536  {
537  foreach($embedded_images AS $array) {
538  if(array_key_exists('path', $array) && array_key_exists('cid', $array))
539  {
540  $tmp_name = array_key_exists('name', $array) && $array['name'] ? $array['name'] : '';
541 
542  $mail->addEmbeddedImage($array['path'], $array['cid'], $tmp_name);
543  }
544 
545  }
546  }
547 
548  // Allegati
549  if(count($attachments))
550  {
551  foreach($attachments AS $array) {
552 
553  if(array_key_exists('path', $array))
554  {
555  $tmp_name = array_key_exists('name', $array) && $array['name'] ? $array['name'] : '';
556 
557  $mail->AddAttachment($array['path'], $tmp_name);
558  }
559  }
560  }
561 
562  if($automatic_html) {
563  $mail->msgHTML($contents);
564  }
565 
566  if($mail->Send()) {
567  return true;
568  }
569  else {
570  if($debug != 0)
571  {
572  echo 'Message was not sent.';
573  echo 'Mailer Error: '.$mail->ErrorInfo;
574  }
575  return false;
576  }
577  }
578 }