Exemple #1
0
 public static function sentEmail($setFromEmail, $setFromName, $emailDestination, $nameDestination, $subject, $msg, $category = 0, $cc = null, $attachment = null)
 {
     /*
     	list category
     	0=common, 1=registration, 2=vacancy, 3=test_call, 4=message, 5=finance
     */
     Yii::import('ext.jphpmailer.JPhpMailer');
     Yii::import('application.modules.email.models.CcnSendEmailLog');
     $mail = new JPhpMailer();
     //log email into database
     $emailLog = new CcnSendEmailLog();
     $emailLog->category = $category;
     $emailLog->date_inserted = date('Y-m-d H:i:s');
     $emailLog->date_sent = date('Y-m-d H:i:s');
     $emailLog->header_from = $setFromEmail;
     $emailLog->email_to = $emailDestination;
     $emailLog->subject = $subject;
     $emailLog->msg = $msg;
     if ($_SERVER["HTTP_HOST"] == 'localhost' || $_SERVER['SERVER_ADDR'] == '192.168.1.250') {
         //in localhost or testing condition
         //smtp google
         $mail->IsSMTP();
         $mail->SMTPSecure = "ssl";
         $mail->SMTPAuth = true;
         // enable SMTP authentication
         $mail->Host = "smtp.gmail.com";
         // sets the SMTP server
         $mail->Port = 465;
         // set the SMTP port for the GMAIL server
         $mail->Username = "******";
         // SMTP account username
         $mail->Password = "******";
         $mail->SetFrom($setFromEmail, $setFromName);
         $mail->Subject = $subject;
         $mail->MsgHTML($msg);
         //$options  = WebOption::model()->findByPk(1);
         //$emailDestination = $options->email_testing;
         $mail->AddAddress($emailDestination, $nameDestination);
         if ($cc != null && count($cc) > 0) {
             foreach ($cc as $to => $name) {
                 $mail->AddAddress($to, $name);
             }
         }
         if ($attachment != null) {
             $mail->addAttachment($attachment);
         }
     } else {
         //live server
         $mail->IsMail();
         $mail->SetFrom($setFromEmail, $setFromName);
         $mail->Subject = $subject;
         $mail->MsgHTML($msg);
         $mail->AddAddress($emailDestination, $nameDestination);
         $mail->AddReplyTo($setFromEmail, $setFromName);
         if ($attachment != null) {
             $mail->addAttachment($attachment);
         }
         if ($cc != null && count($cc) > 0) {
             foreach ($cc as $to => $name) {
                 $mail->AddAddress($to, $name);
             }
         }
     }
     //	file_put_contents('assets/cek_email.html', $msg);
     if ($_SERVER["HTTP_HOST"] == 'localhost') {
         $emailLog->is_sended = 1;
         $emailLog->save();
         $file = fopen('assets/localhost_email_' . $emailDestination . '.html', 'w+');
         fwrite($file, $msg);
         fclose($file);
         return true;
     } else {
         if ($mail->Send()) {
             $emailLog->is_sended = 1;
             $emailLog->save();
             return true;
         } else {
             $emailLog->is_sended = 0;
             $emailLog->save();
             return false;
         }
     }
 }
 public static function send($to, $subject = null, $body = null, $header = null)
 {
     if ($to instanceof YumUser) {
         $to = $to->profile->email;
     }
     if (!is_array($to)) {
         $to = array('to' => $to, 'subject' => $subject, 'body' => $body);
     }
     if (Yum::module()->mailer == 'swift') {
         $sm = Yii::app()->swiftMailer;
         $mailer = $sm->mailer($sm->mailTransport());
         $message = $sm->newMessage($to['subject'])->setFrom($to['from'])->setTo($to['to'])->setBody($to['body']);
         return $mailer->send($message);
     } else {
         if (Yum::module()->mailer == 'PHPMailer') {
             Yii::import('application.extensions.phpmailer.JPhpMailer');
             $mailer = new JPhpMailer(true);
             if (Yum::module()->phpmailer['transport']) {
                 switch (Yum::module()->phpmailer['transport']) {
                     case 'smtp':
                         $mailer->IsSMTP();
                         break;
                     case 'sendmail':
                         $mailer->IsSendmail();
                         break;
                     case 'qmail':
                         $mailer->IsQmail();
                         break;
                     case 'mail':
                     default:
                         $mailer->IsMail();
                 }
             } else {
                 $mailer->IsMail();
             }
             if (Yum::module()->phpmailer['html']) {
                 $mailer->IsHTML(Yum::module()->phpmailer['html']);
             } else {
                 $mailer->IsHTML(false);
             }
             $mailerconf = Yum::module()->phpmailer['properties'];
             if (is_array($mailerconf)) {
                 foreach ($mailerconf as $key => $value) {
                     if (isset(JPhpMailer::${$key})) {
                         JPhpMailer::${$key} = $value;
                     } else {
                         $mailer->{$key} = $value;
                     }
                 }
             }
             $mailer->SetFrom($to['from'], Yum::module()->phpmailer['msgOptions']['fromName']);
             //FIXME
             $mailer->AddAddress($to['to'], Yum::module()->phpmailer['msgOptions']['toName']);
             //FIXME
             $mailer->Subject = $to['subject'];
             $mailer->Body = $to['body'];
             return $mailer->Send();
         } else {
             if ($header == null) {
                 $header = 'MIME-Version: 1.0' . "\r\n";
                 $header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
                 $header .= 'From: ' . Yum::module('message')->adminEmail . "\r\n";
                 $header .= 'To: ' . $to['to'] . "\r\n";
             }
             return mail($to['to'], $to['subject'], $to['body'], $header);
         }
     }
 }
 /**
  * Sent Email
  */
 public static function sendEmail($to_email, $to_name, $subject, $message, $type, $cc = null, $attachment = null)
 {
     ini_set('max_execution_time', 0);
     ob_start();
     Yii::import('application.extensions.phpmailer.JPhpMailer');
     $model = self::model()->findByPk(1, array('select' => 'mail_contact, mail_name, mail_from, mail_smtp, smtp_address, smtp_port, smtp_username, smtp_password, smtp_ssl'));
     $mail = new JPhpMailer();
     if ($model->mail_smtp == 1 || $_SERVER["SERVER_ADDR"] == '127.0.0.1' || $_SERVER["HTTP_HOST"] == 'localhost') {
         //in localhost or testing condition
         //smtp google
         $mail->IsSMTP();
         // Set mailer to use SMTP
         $mail->Host = $model->smtp_address;
         // Specify main and backup server
         $mail->Port = $model->smtp_port;
         // set the SMTP port
         $mail->SMTPAuth = true;
         // Enable SMTP authentication
         $mail->Username = $model->smtp_username;
         // SES SMTP  username
         $mail->Password = $model->smtp_password;
         // SES SMTP password
         if ($model->smtp_ssl != 0) {
             $mail->SMTPSecure = $model->smtp_ssl == 1 ? "tls" : "ssl";
         }
         // Enable encryption, 'ssl' also accepted
     } else {
         //live server
         $mail->IsMail();
     }
     /**
      * 0 = to admin
      * 1 = to user
      */
     if ($type == 0) {
         $mail->SetFrom($to_email, $to_name);
         $mail->AddReplyTo($to_email, $to_name);
         $mail->AddAddress($model->mail_contact, $model->mail_name);
     } else {
         $mail->SetFrom($model->mail_from, $model->mail_name);
         $mail->AddReplyTo($model->mail_from, $model->mail_name);
         $mail->AddAddress($to_email, $to_name);
     }
     // cc
     if ($cc != null && count($cc) > 0) {
         foreach ($cc as $email => $name) {
             $mail->AddAddress($email, $name);
         }
     }
     // attachment
     if ($attachment != null) {
         $mail->addAttachment($attachment);
     }
     $mail->Subject = $subject;
     $mail->MsgHTML($message);
     if ($mail->Send()) {
         return true;
         //echo 'send';
     } else {
         return false;
         //echo 'no send';
     }
     ob_end_flush();
 }