Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise default SMTP timeout from 10 to 30 sec #39638

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 24 additions & 38 deletions lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,7 @@ public function send(IMessage $message): array {
$failedRecipients = [];

array_walk($recipients, function ($value, $key) use (&$failedRecipients) {
if (is_numeric($key)) {
$failedRecipients[] = $value;
} else {
$failedRecipients[] = $key;
}
$failedRecipients[] = is_numeric($key) ? $value : $key;
});

return $failedRecipients;
Expand All @@ -204,11 +200,7 @@ public function send(IMessage $message): array {
$failedRecipients = [];

array_walk($recipients, function ($value, $key) use (&$failedRecipients) {
if (is_numeric($key)) {
$failedRecipients[] = $value;
} else {
$failedRecipients[] = $key;
}
$failedRecipients[] = is_numeric($key) ? $value : $key;
});

return $failedRecipients;
Expand Down Expand Up @@ -244,17 +236,12 @@ protected function getInstance(): MailerInterface {
return $this->instance;
}

$transport = null;
$transportMode = $this->config->getSystemValueString('mail_smtpmode', 'smtp');

switch ($this->config->getSystemValueString('mail_smtpmode', 'smtp')) {
case 'sendmail':
$transport = $this->getSendMailInstance();
break;
case 'smtp':
default:
$transport = $this->getSmtpInstance();
break;
}
$transport = match ($transportMode) {
'sendmail' => $this->getSendMailInstance(),
default => $this->getSmtpInstance(),
};

return new SymfonyMailer($transport);
}
Expand All @@ -269,19 +256,23 @@ protected function getInstance(): MailerInterface {
* @return EsmtpTransport
*/
protected function getSmtpInstance(): EsmtpTransport {
$mailSmtpHost = $this->config->getSystemValueString('mail_smtphost', '127.0.0.1');
$mailSmtpPort = $this->config->getSystemValueInt('mail_smtpport', 25);
// either null or true - if nothing is passed, let the symfony mailer figure out the configuration by itself
$mailSmtpsecure = ($this->config->getSystemValue('mail_smtpsecure', null) === 'ssl') ? true : null;
$mailSmtpSecure = ($this->config->getSystemValue('mail_smtpsecure', null) === 'ssl') ? true : null;

$transport = new EsmtpTransport(
$this->config->getSystemValueString('mail_smtphost', '127.0.0.1'),
$this->config->getSystemValueInt('mail_smtpport', 25),
$mailSmtpsecure,
$mailSmtpHost,
$mailSmtpPort,
$mailSmtpSecure,
null,
$this->logger
);

/** @var SocketStream $stream */
$stream = $transport->getStream();
/** @psalm-suppress InternalMethod */
$stream->setTimeout($this->config->getSystemValueInt('mail_smtptimeout', 10));
$stream->setTimeout($this->config->getSystemValueInt('mail_smtptimeout', 30));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the downside is that server that are not reachable will make the PHP process run longer than 30 seconds and cause a timeout

I'm on the fence here. we could also improve our documentation about increasing the timeout if there are servers where this is necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I would also vote against it, timeout can already be set in the configuration and 10 seconds looks like a sane default.


if ($this->config->getSystemValueBool('mail_smtpauth', false)) {
$transport->setUsername($this->config->getSystemValueString('mail_smtpname', ''));
Expand Down Expand Up @@ -316,21 +307,16 @@ protected function getSmtpInstance(): EsmtpTransport {
*
* @return SendmailTransport
*/

protected function getSendMailInstance(): SendmailTransport {
switch ($this->config->getSystemValueString('mail_smtpmode', 'smtp')) {
case 'qmail':
$binaryPath = '/var/qmail/bin/sendmail';
break;
default:
$sendmail = \OCP\Server::get(IBinaryFinder::class)->findBinaryPath('sendmail');
if ($sendmail === null) {
$sendmail = '/usr/sbin/sendmail';
}
$binaryPath = $sendmail;
break;
}
$smtpMode = $this->config->getSystemValueString('mail_smtpmode', 'smtp');
$binaryPath = match ($smtpMode) {
'qmail' => '/var/qmail/bin/sendmail',
default => \OCP\Server::get(IBinaryFinder::class)->findBinaryPath('sendmail') ?? '/usr/sbin/sendmail',
};

$binaryParam = match ($this->config->getSystemValueString('mail_sendmailmode', 'smtp')) {
$sendmailMode = $this->config->getSystemValueString('mail_sendmailmode', 'smtp');
$binaryParam = match ($sendmailMode) {
'pipe' => ' -t -i',
default => ' -bs',
};
Expand Down
Loading