Skip to content

Commit

Permalink
SmtpMailer: allow setting the client name through config (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
janbuchar authored and dg committed Sep 13, 2017
1 parent 707fe7d commit f3bc518
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Bridges/MailDI/MailExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MailExtension extends Nette\DI\CompilerExtension
{
public $defaults = [
'smtp' => false,
'clientHostname' => null,
'host' => null,
'port' => null,
'username' => null,
Expand Down
12 changes: 11 additions & 1 deletion src/Mail/SmtpMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
class SmtpMailer implements IMailer
{
use Nette\SmartObject;

/** @var string */
private $clientHostname;

/** @var resource */
private $connection;
Expand Down Expand Up @@ -56,6 +59,7 @@ public function __construct(array $options = [])
$this->host = ini_get('SMTP');
$this->port = (int) ini_get('smtp_port');
}
$this->clientHostname = isset($options['clientHostname']) ? $options['clientHostname'] : null;
$this->username = $options['username'] ?? '';
$this->password = $options['password'] ?? '';
$this->secure = $options['secure'] ?? '';
Expand Down Expand Up @@ -130,7 +134,13 @@ protected function connect(): void
stream_set_timeout($this->connection, $this->timeout, 0);
$this->read(); // greeting

$self = isset($_SERVER['HTTP_HOST']) && preg_match('#^[\w.-]+\z#', $_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
$self = $this->clientHostname;
if ($self === null) {
$self = isset($_SERVER['HTTP_HOST']) && preg_match('#^[\w.-]+\z#', $_SERVER['HTTP_HOST'])
? $_SERVER['HTTP_HOST']
: 'localhost';
}

$this->write("EHLO $self");
$ehloResponse = $this->read();
if ((int) $ehloResponse !== 250) {
Expand Down

0 comments on commit f3bc518

Please sign in to comment.