public function loadModel($id) { $m = MllgMailerLog::model(); // apply scope, if available $scopes = $m->scopes(); if (isset($scopes[$this->scope])) { $m->{$this->scope}(); } $model = $m->findByPk($id); if ($model === null) { throw new CHttpException(404, Yii::t('D2mailerModule.crud', 'The requested page does not exist.')); } return $model; }
/** * send email to user by smtp * @param int $user_id * @param string $subject * @param string $message * @param string $from_email * @param string $from_name * @return boolean/string false on error, string on success */ public function sendMailToUser($user_id, $subject, $message, $from_email = false, $from_name = false) { $this->error = false; if (!$from_email) { $from_email = $this->fromEmail; } if (!$from_name) { $from_name = $this->fromName; } $user = User::model()->findbyPk($user_id); $user_full_name = $user->profile->first_name . ' ' . $user->profile->last_name; //validate if (empty($user->email)) { $this->error = Yii::t('D2mailerModule.errors', 'User don\'t have email: ') . $user_full_name; return false; } //create message //Yii::import('vendor.swiftmailer.swiftmailer.lib.classes.Swift.*'); $swiftMessage = Swift_Message::newInstance($subject); $swiftMessage->setBody($message, 'text/html'); $swiftMessage->setFrom($from_email, $from_name); $swiftMessage->setTo($user->email, $user_full_name); if ($this->logging && $this->logging_model_name && $this->logging_model_id) { $mllg = new MllgMailerLog(); $mllg->mllg_model_name = $this->logging_model_name; $mllg->mllg_model_id = $this->logging_model_id; $mllg->mllg_datetime = new CDbExpression('NOW()'); if (!$this->isConcole()) { $mllg->mllg_user_id = Yii::app()->user->id; } $mllg->mllg_to = $user->email; $mllg->mllg_subject = $subject; $mllg->mllg_text = $message; $mllg->mllg_text_format = MllgMailerLog::MLLG_TEXT_FORMAT_TEXTHTML; $mllg->mllg_status = MllgMailerLog::MLLG_STATUS_SENT; } /** * Create the Mailer and Send * @link http://swiftmailer.org/docs/sending.html */ // Create the Transport if (!$this->smtp_username) { $transport = Swift_SmtpTransport::newInstance($this->smtp_host, $this->smtp_port); } else { $transport = Swift_SmtpTransport::newInstance($this->smtp_host, $this->smtp_port)->setUsername($this->smtp_username)->setPassword($this->smtp_password); } //create Mauler and send if (!Swift_Mailer::newInstance($transport)->send($swiftMessage)) { $error_text = Yii::t('D2personModule.model', 'Can not send email to ') . $user_full_name . ' ' . $user->email; $this->error = $error_text; Yii::log(CLogger::LEVEL_ERROR, $error_text); if ($this->logging && $this->logging_model_name && $this->logging_model_id) { $mllg->mllg_status = MllgMailerLog::MLLG_STATUS_ERROR; $mllg->save(); } return false; } if ($this->logging && $this->logging_model_name && $this->logging_model_id) { $mllg->mllg_status = MllgMailerLog::MLLG_STATUS_SENT; $mllg->save(); } return $user_full_name . ' ' . $user->email; }