/** * Deletes a particular model. * If deletion is successful, the browser will be redirected to the 'admin' page. * @param integer $id the ID of the model to be deleted */ public function actionDelete($id) { $this->hasPrivilege(Acl::ACTION_DELETE); EmailQueue::model()->loadModel($id)->delete(); if (!isset($_GET['ajax'])) { $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index')); } }
public static function sendEmail($jobId) { $model = EmailQueue::model()->findByPk($jobId); if ($model && $model->sent) { return false; } else { // Send Email Here } }
/** * Try sending e-mails in the queue. * * @return void * @throws CDbException */ private function _sendQueueEmails() { $objMails = EmailQueue::model()->findAll("`sent_attempts` < 20 and `sent_attempts` > 0 and `to` IS NOT NULL LIMIT 10"); foreach ($objMails as $objMail) { $blnResult = _xls_send_email($objMail->id, true); if (!$blnResult) { EmailQueue::model()->updateByPk($objMail->id, array('sent_attempts' => $objMail->sent_attempts + 1)); } else { $objMail->delete(); } } }
public function actionSend($frequency = 55) { $endTime = time() + $frequency; // Check if a previous job has been running for the past 10 minutes. if (!DaemonUtils::checkForSync('EMAIL')) { DaemonUtils::closeJobForSync('EMAIL'); die; } $job = DaemonUtils::addJob('EMAIL'); try { $count = 0; while (($emails = EmailQueue::model()->findAll('sent=:sent', array(':sent' => '0'))) && !empty($emails) && time() < $endTime) { foreach ($emails as $email) { if (time() < $endTime) { if (!$email->sent) { $result = EmailApi::sendSmtpEmail($email); if ($result == 1) { $email->sent = 1; } else { $email->attempts++; } $email->save(); $count++; } } else { break; } } } /*while(!empty($emails) && time()<$endTime){ $result = EmailApi::sendSmtpEmails($emails); }*/ $message = "{$count} email(s) were processed."; // $message = count($emails) . " email(s) were processed."; DaemonUtils::endJob($job, $message); } catch (Exception $e) { $message = $e->getMessage(); DaemonUtils::endJob($job, $message, 'ERROR'); } }
/** * Send any emails that are still pending * * @param $intCartid * @return void */ public static function sendEmails($intCartid) { $objEmails = EmailQueue::model()->findAllByAttributes(array('cart_id' => $intCartid)); Yii::log(count($objEmails) . " emails to be sent", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); foreach ($objEmails as $objEmail) { _xls_send_email($objEmail->id, true); } }
function _xls_send_email($id, $hideJson = false) { $objMail = EmailQueue::model()->findByPk($id); if ($objMail instanceof EmailQueue) { $orderEmail = _xls_get_conf('ORDER_FROM', ''); $from = empty($orderEmail) ? _xls_get_conf('EMAIL_FROM') : $orderEmail; Yii::app()->setComponent('Smtpmail', null); $mail = Yii::app()->Smtpmail; //$mail->CharSet="utf-8"; $mail->Debugoutput = "error_log"; $mail->IsSMTP(); $mail->Username = Yii::app()->params['EMAIL_SMTP_USERNAME']; $mail->Password = _xls_decrypt(Yii::app()->params['EMAIL_SMTP_PASSWORD']); $mail->Mailer = 'smtp'; $mail->Port = Yii::app()->params['EMAIL_SMTP_PORT']; $SMTPSecure = ""; if (Yii::app()->params['EMAIL_SMTP_SECURITY_MODE'] == '0') { if (Yii::app()->params['EMAIL_SMTP_PORT'] == "465") { $SMTPSecure = "ssl"; } if (Yii::app()->params['EMAIL_SMTP_PORT'] == "587") { $SMTPSecure = "tls"; } } if (_xls_get_conf('EMAIL_SMTP_SECURITY_MODE') == '1') { $SMTPSecure = ""; } if (_xls_get_conf('EMAIL_SMTP_SECURITY_MODE') == '2') { $SMTPSecure = "ssl"; } if (_xls_get_conf('EMAIL_SMTP_SECURITY_MODE') == '3') { $SMTPSecure = "tls"; } $mail->SMTPAuth = true; $mail->AuthType = "LOGIN"; if (_xls_get_conf('EMAIL_SMTP_AUTH_PLAIN', '0') == '1') { $mail->AuthType = "PLAIN"; } if (empty(Yii::app()->params['EMAIL_SMTP_PASSWORD'])) { Yii::log("Password for SMTP blank, turning off SMTP Authentication", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $mail->SMTPAuth = false; $mail->Username = ''; $mail->Password = ''; } $mail->SMTPDebug = 1; $mail->SMTPSecure = $SMTPSecure; $mail->Host = Yii::app()->params['EMAIL_SMTP_SERVER']; $mail->SetFrom($from, Yii::app()->params['STORE_NAME']); $mail->Subject = $objMail->subject; $mail->ClearAllRecipients(); $mail->AddAddress($objMail->to); if (!empty(Yii::app()->params['EMAIL_BCC'])) { if ($objMail->to != Yii::app()->params['EMAIL_BCC'] && $objMail->to == $from) { $mail->AddCC(Yii::app()->params['EMAIL_BCC']); } } $mail->MsgHTML($objMail->htmlbody); $blnResult = $mail->Send(); $mail->Password = '******'; //replace the real password before logging Yii::log("Contents of mail " . print_r($mail, true), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); if ($blnResult) { Yii::log("Sent email to " . $objMail->to . " successfully.", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $objMail->delete(); Yii::log("Email removed from queue", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); if (!$hideJson) { echo json_encode("success"); } } else { $objMail->sent_attempts += 1; $objMail->save(); Yii::log("Sending email failed ID " . $id . " " . $objMail->to . " " . print_r($mail->ErrorInfo, true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); if (!$hideJson) { echo json_encode("failure"); } } } return $blnResult; }