Esempio n. 1
0
 public static function send($recipient, $subject, $message, $cc = null, $bcc = null, $signature = "Thanks,<br/></br>ZEITI Portal")
 {
     try {
         $mail = new PHPMailer(true);
         $mail->isSMTP();
         //$mail->SMTPDebug = 2;
         //$mail->Debugoutput = 'html';// Set mailer to use SMTP
         $mail->Host = 'smtp.gmail.com';
         //'smtp.zamnet.zm';  // Specify main and backup SMTP servers
         $mail->SMTPAuth = true;
         // Enable SMTP authentication
         $mail->Username = '******';
         //;'*****@*****.**';                 // SMTP username
         $mail->Password = '******';
         //'abcd34';                           // SMTP password
         $mail->SMTPSecure = 'tls';
         //''                            // Enable TLS encryption, `ssl` also accepted
         $mail->Port = 587;
         //25;
         $mail->isHTML(true);
         $mail->Subject = $subject;
         if (is_array($recipient) && array_key_exists('name', $recipient)) {
             $name = $recipient['name'];
             $mail->Body = "<p>Dear {$name},</p>" . "<p>{$message}</p>" . "<p>{$signature}</p>";
         } else {
             $mail->Body = "<p>Dear user,</p>" . "<p>{$message}</p>" . "<p>{$signature}</p>";
         }
         //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
         $mail->setFrom('*****@*****.**', 'ZEITI Portal Team');
         //$mail->addReplyTo('*****@*****.**', 'Information');
         if (ZP::isEmail($recipient)) {
             $mail->addAddress($recipient);
         } else {
             if (is_array($recipient)) {
                 if (array_key_exists('email', $recipient)) {
                     if (array_key_exists('name', $recipient)) {
                         $mail->addAddress($recipient['email'], $recipient['name']);
                     } else {
                         $mail->addAddress($recipient['email']);
                     }
                 } else {
                     foreach ($recipient as $value) {
                         if (array_key_exists('email', $value)) {
                             if (array_key_exists('name', $value)) {
                                 $mail->addAddress($value['email'], $value['name']);
                             } else {
                                 $mail->addAddress($value['email']);
                             }
                         }
                     }
                 }
             } else {
                 return ['success' => false, 'error' => 'Invalid recipient specified.'];
             }
         }
         // if cc has been specified
         if (!is_null($cc)) {
             if (array_key_exists('email', $cc)) {
                 if (array_key_exists('name', $cc)) {
                     $mail->addCC($cc['email'], $cc['name']);
                 } else {
                     $mail->addCC($cc['email']);
                 }
             } else {
                 foreach ($cc as $value) {
                     if (array_key_exists('email', $value)) {
                         if (array_key_exists('name', $value)) {
                             $mail->addCC($value['email'], $value['name']);
                         } else {
                             $mail->addCC($value['email']);
                         }
                     }
                 }
             }
         }
         // if bcc has been specified
         if (!is_null($bcc)) {
             if (array_key_exists('email', $bcc)) {
                 if (array_key_exists('name', $bcc)) {
                     $mail->addBCC($bcc['email'], $bcc['name']);
                 } else {
                     $mail->addBCC($bcc['email']);
                 }
             } else {
                 foreach ($bcc as $value) {
                     if (array_key_exists('email', $value)) {
                         if (array_key_exists('name', $value)) {
                             $mail->addBCC($value['email'], $value['name']);
                         } else {
                             $mail->addBCC($value['email']);
                         }
                     }
                 }
             }
         }
         return ['success' => $mail->send()];
     } catch (\phpmailerException $e) {
         return ['success' => false];
     }
 }