Skip to content

Commit

Permalink
Merge pull request #5 from bakaphp/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
kaioken authored Mar 7, 2019
2 parents 537fd9a + 72d9015 commit a4a097a
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class Message extends \Phalcon\Mailer\Message
*/
public function content($content, $contentType = self::CONTENT_TYPE_HTML, $charset = null)
{
if (isset($this->params) && is_array($this->params)) {
$content = $this->setDynamicContent($this->params, $content);
}

$this->getMessage()->setBody($content, $contentType, $charset);

return $this;
Expand Down Expand Up @@ -105,16 +109,16 @@ public function sendNow()
$password = $config->email->password;
$host = $config->email->host;
$port = $config->email->port;

$transport = \Swift_SmtpTransport::newInstance($host, $port);

$transport->setUsername($username);
$transport->setPassword($password);

$swift = \Swift_Mailer::newInstance($transport);

$failures = [];

$swift->send($message, $failures);
}

Expand All @@ -128,11 +132,11 @@ public function smtp(array $params)
{
//validate the user params
if (!array_key_exists('username', $params)) {
throw new Exception("We need a username");
throw new Exception('We need a username');
}

if (!array_key_exists('password', $params)) {
throw new Exception("We need a password");
throw new Exception('We need a password');
}

$this->smtp = $params;
Expand Down Expand Up @@ -200,4 +204,23 @@ public function template($template = 'email.volt')

return $this;
}

/**
* Set content dynamically by params
* @param $params
* @param $content
* @return string
*/
public function setDynamicContent(array $params, string $content)
{
$processed_content = preg_replace_callback(
'~\{(.*?)\}~si',
function ($match) use ($params) {
return str_replace($match[0], isset($params[$match[1]]) ? $params[$match[1]] : $match[0], $match[0]);
},
$content
);

return $processed_content;
}
}

0 comments on commit a4a097a

Please sign in to comment.