Last active
December 21, 2015 23:49
-
-
Save OndrejSlamecka/6384756 to your computer and use it in GitHub Desktop.
Sending simple emails should be easier in Nette Framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
common: | |
services: | |
mailer: App\Mailer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// $mailer instanceof App\Mailer === true | |
$mailer->easySend('admin@example.com', 'user@example.com', __DIR__ . '/hello.latte', ['greeting' => 'Howdy']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class Mailer | |
{ | |
/** @var \Nette\Mail\IMailer @inject */ | |
public $mailer; | |
public function easySend($from, $to, $templatePath, $templateValues = []) | |
{ | |
// Template | |
$template = new \Nette\Templating\FileTemplate($templatePath); | |
$template->registerFilter(new \Nette\Latte\Engine); | |
foreach ($templateValues as $key => $val) { | |
$template->$key = $val; | |
} | |
$mail = new \Nette\Mail\Message(); | |
$mail->setFrom($from)->setHtmlBody($template); | |
foreach ((array) $to as $t) { | |
$mail->addTo($t); | |
} | |
$this->mailer->send($mail); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Děláš modelovou třídu, bylo by mnohem lepší použít konstruktor injection.