Skip to content

Commit af38f34

Browse files
janbuchardg
authored andcommitted
SmtpMailer: allow setting the client name through config (#46)
1 parent 707fe7d commit af38f34

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Bridges/MailDI/MailExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class MailExtension extends Nette\DI\CompilerExtension
2626
'secure' => null,
2727
'timeout' => null,
2828
'context' => null,
29+
'clientHost' => null,
2930
];
3031

3132

src/Mail/SmtpMailer.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class SmtpMailer implements IMailer
4646
/** @var bool */
4747
private $persistent;
4848

49+
/** @var string */
50+
private $clientHost;
51+
4952

5053
public function __construct(array $options = [])
5154
{
@@ -65,6 +68,13 @@ public function __construct(array $options = [])
6568
$this->port = $this->secure === 'ssl' ? 465 : 25;
6669
}
6770
$this->persistent = !empty($options['persistent']);
71+
if (isset($options['clientHost'])) {
72+
$this->clientHost = $options['clientHost'];
73+
} else {
74+
$this->clientHost = isset($_SERVER['HTTP_HOST']) && preg_match('#^[\w.-]+\z#', $_SERVER['HTTP_HOST'])
75+
? $_SERVER['HTTP_HOST']
76+
: 'localhost';
77+
}
6878
}
6979

7080

@@ -130,19 +140,18 @@ protected function connect(): void
130140
stream_set_timeout($this->connection, $this->timeout, 0);
131141
$this->read(); // greeting
132142

133-
$self = isset($_SERVER['HTTP_HOST']) && preg_match('#^[\w.-]+\z#', $_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
134-
$this->write("EHLO $self");
143+
$this->write("EHLO $this->clientHost");
135144
$ehloResponse = $this->read();
136145
if ((int) $ehloResponse !== 250) {
137-
$this->write("HELO $self", 250);
146+
$this->write("HELO $this->clientHost", 250);
138147
}
139148

140149
if ($this->secure === 'tls') {
141150
$this->write('STARTTLS', 220);
142151
if (!stream_socket_enable_crypto($this->connection, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
143152
throw new SmtpException('Unable to connect via TLS.');
144153
}
145-
$this->write("EHLO $self", 250);
154+
$this->write("EHLO $this->clientHost", 250);
146155
}
147156

148157
if ($this->username != null && $this->password != null) {

0 commit comments

Comments
 (0)