Skip to content

Instantly share code, notes, and snippets.

@OndrejSlamecka
Last active December 21, 2015 23:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OndrejSlamecka/6384756 to your computer and use it in GitHub Desktop.
Save OndrejSlamecka/6384756 to your computer and use it in GitHub Desktop.
Sending simple emails should be easier in Nette Framework.
common:
services:
mailer: App\Mailer
<?php
// $mailer instanceof App\Mailer === true
$mailer->easySend('admin@example.com', 'user@example.com', __DIR__ . '/hello.latte', ['greeting' => 'Howdy']);
<?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
$mail = new \Nette\Mail\Message();
$mail->setFrom($from)->setHtmlBody($template);
foreach ((array) $to as $t) {
$mail->addTo($t);
}
$this->mailer->send($mail);
}
}
@fprochazka
Copy link

Děláš modelovou třídu, bylo by mnohem lepší použít konstruktor injection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment