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; } } }
/** * 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(); }