Skip to content

Commit

Permalink
fixed mailTrait
Browse files Browse the repository at this point in the history
nb. add parse template, subject and body message
  • Loading branch information
PutraSudaryanto committed May 29, 2018
1 parent 085add8 commit d7ec3bf
Showing 1 changed file with 104 additions and 16 deletions.
120 changes: 104 additions & 16 deletions MailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,128 @@
* @link https://github.com/ommu/yii2-traits
*
* Contains many function that most used :
* mailMessage
* parseTemplate
*
* getMailFrom
* getMailTemplatePath
* getMailMessage
* parseMailSubject
* parseMailBody
*
*/

namespace ommu\traits;

use Yii;
use yii\helpers\Html;
use app\models\CoreMailSetting;
use app\models\CoreMailTemplate;

trait MailTrait
{
/**
* mailMessage
* getMailFrom
*
* @return array
*/
public function getMailFrom()
{
$model = CoreMailSetting::find()
->select(['mail_name','mail_from'])
->where(['id' => 1])
->one();

if($model == null)
return false;

$mail_name = $model->mail_name ? $model->mail_name : $model->mail_from;
return [$model->mail_from => $mail_name];
}

/**
* getMailTemplatePath
*
* @return string
*/
public function mailMessage($template, $attributes=null)
public function getMailTemplatePath()
{
$templatePath = join('/', [Yii::getAlias('@public'), 'email', 'template']);

return $templatePath;
}

/**
* getMailMessage
*
* @param string $template
* @return string
*/
public function getMailMessage($template)
{
$template = $template.'.php';
$templateFile = join('/', [$this->getMailTemplatePath(), $template]);

$message = '';
if(file_exists($templateFile))
$message = file_get_contents($templateFile);

return $message;
}

/**
* Method for parsing string
*
* @param string $message Source string for parsing
* @param array $attribute of example
* [
* '{link}' => 'https://github.com/black-lamp/yii2-email-templates',
* '{author}' => Yii::$app->user->email
* ]
*
* @return string
*/
public function parseTemplate($message, $attribute)
{
foreach ($attribute as $key => $value) {
$message = strtr($message, [
'{'.$key.'}' => $value,
]);
}

return $message;
}

/**
* Parsing of email subject
*
* @return string
*/
public function parseMailSubject($template)
{
$model = CoreMailTemplate::find()
->select(['subject'])
->where(['template' => $template])
->one();

$message = $this->parseTemplate($model->subject, ['sitename' => Yii::$app->name]);

$templateCode = CoreMailTemplate::find()
->select(['template_code'])
return $message;
}

/**
* Parsing of email body
*
* @return string
*/
public function parseMailBody($template, $attributes=null)
{
$model = CoreMailTemplate::find()
->select(['template_file'])
->where(['template' => $template])
->one();

$messageBody = file_get_contents(join('/', [$templatePath, $templateCode->template_code.'.php']));
$messageSearch = $messageReplace = [];
if($attributes && is_array($attributes) && !empty($attributes)) {
foreach ($attributes as $key => $val) {
$messageSearch[] = '{'.$key.'}';
$messageReplace[] = $val;
}
return str_ireplace($messageSearch, $messageReplace, $messageBody);

} else
return $messageBody;
$message = $this->parseTemplate($this->getMailMessage($model->template_file), $attributes);

return $message;
}
}

0 comments on commit d7ec3bf

Please sign in to comment.