0) { $r = fwrite($fh, $str); if(!$r) throw new Exception("write error"); $str = substr($str, $r); $len -= $r; } } function safe_fprintf($fh, ...$args) { safe_fwrite($fh, call_user_func_array('sprintf', $args)); } class SendmailMailer extends Nette\Object { /** @inject @var \Nette\DI\Container */ public $container; public function createMessage(...$args) { $rc = Nette\Reflection\ClassType::from('App\Model\MailMessage'); assert('$rc->instantiable'); $model = $rc->newInstanceArgs($args); $this->container->callInjects($model); return $model; } public function send(App\Model\MailMessage $mail) { try { $from = $mail->envelopeFrom; $to = $mail->envelopeTo; putenv("PHP_SENDMAIL_FROM=$from"); putenv("PHP_SENDMAIL_TO=$to"); $sendmail = popen('exec /usr/lib/sendmail -oi -f "$PHP_SENDMAIL_FROM" "$PHP_SENDMAIL_TO"', 'w'); safe_fwrite($sendmail, $mail->generateMessage()); if(pclose($sendmail)) throw new Exception("write error"); } finally { putenv("PHP_SENDMAIL_FROM"); putenv("PHP_SENDMAIL_TO"); } } }