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

[stable15] Add pipe mode for sendmail #12736

Merged
merged 3 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,19 @@
*/
'mail_send_plaintext_only' => false,

/**
* Which mode is used for sendmail/qmail: ``smtp`` or ``pipe``.
*
* For ``smtp`` the sendmail binary is started with the parameter ``-bs``:
* - Use the SMTP protocol on standard input and output.
*
* For ``pipe`` the binary is started with the parameters ``-t``:
* - Read message from STDIN and extract recipients.
*
* Defaults to ``smtp``
*/
'mail_sendmailmode' => 'smtp',

/**
* Proxy Configurations
*/
Expand Down
11 changes: 10 additions & 1 deletion lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ protected function getSendMailInstance(): \Swift_SendmailTransport {
break;
}

return new \Swift_SendmailTransport($binaryPath . ' -bs');
switch ($this->config->getSystemValue('mail_sendmailmode', 'smtp')) {
case 'pipe':
$binaryParam = ' -t';
break;
default:
$binaryParam = ' -bs';
break;
}

return new \Swift_SendmailTransport($binaryPath . $binaryParam);
}
}
1 change: 1 addition & 0 deletions lib/private/Settings/Admin/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function getForm() {
'mail_smtpauth' => $this->config->getSystemValue('mail_smtpauth', false),
'mail_smtpname' => $this->config->getSystemValue('mail_smtpname', ''),
'mail_smtppassword' => $this->config->getSystemValue('mail_smtppassword', ''),
'mail_sendmailmode' => $this->config->getSystemValue('mail_sendmailmode', 'smtp'),
];

if ($parameters['mail_smtppassword'] !== '') {
Expand Down
3 changes: 2 additions & 1 deletion settings/Controller/MailSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public function setMailSettings($mail_domain,
$mail_smtphost,
$mail_smtpauthtype,
$mail_smtpauth,
$mail_smtpport) {
$mail_smtpport,
$mail_sendmailmode) {

$params = get_defined_vars();
$configs = [];
Expand Down
2 changes: 2 additions & 0 deletions settings/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ $(document).ready(function(){
$('#mail_smtpsecure_label').addClass('hidden');
$('#mail_smtpsecure').addClass('hidden');
$('#mail_credentials').addClass('hidden');
$('#mail_sendmailmode_label, #mail_sendmailmode').removeClass('hidden');
} else {
$('#setting_smtpauth').removeClass('hidden');
$('#setting_smtphost').removeClass('hidden');
Expand All @@ -166,6 +167,7 @@ $(document).ready(function(){
if ($('#mail_smtpauth').is(':checked')) {
$('#mail_credentials').removeClass('hidden');
}
$('#mail_sendmailmode_label, #mail_sendmailmode').addClass('hidden');
}
});

Expand Down
14 changes: 14 additions & 0 deletions settings/templates/settings/admin/additional-mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
$mail_smtpmode[] = ['qmail', 'qmail'];
}

$mail_sendmailmode = [
'smtp' => 'smtp (-bs)',
'pipe' => 'pipe (-t)'
];

?>

<div class="section" id="mail_general_settings">
Expand Down Expand Up @@ -84,6 +89,15 @@
<option value="<?php p($secure)?>" <?php p($selected) ?>><?php p($name) ?></option>
<?php endforeach;?>
</select>

<label id="mail_sendmailmode_label" for="mail_sendmailmode" class="<?= $_['mail_smtpmode'] !== 'sendmail' ? 'hidden' : '' ?>">
<?php p($l->t('Sendmail mode')); ?>
</label>
<select name="mail_sendmailmode" id="mail_sendmailmode" class="<?= $_['mail_smtpmode'] !== 'sendmail' ? 'hidden' : '' ?>">
<?php foreach ($mail_sendmailmode as $sendmailmodeValue => $sendmailmodeLabel): ?>
<option value="<?php p($sendmailmodeValue)?>" <?= $sendmailmodeValue === $_['mail_sendmailmode'] ? 'selected="selected"' : '' ?>><?php p($sendmailmodeLabel) ?></option>
<?php endforeach;?>
</select>
</p>

<p>
Expand Down
8 changes: 6 additions & 2 deletions tests/Settings/Controller/MailSettingsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function testSetMailSettings() {
'mail_smtpauthtype' => 'NTLM',
'mail_smtpauth' => 1,
'mail_smtpport' => '25',
'mail_sendmailmode' => null,
]],
[[
'mail_domain' => 'nextcloud.com',
Expand All @@ -86,6 +87,7 @@ public function testSetMailSettings() {
'mail_smtpport' => '25',
'mail_smtpname' => null,
'mail_smtppassword' => null,
'mail_sendmailmode' => null,
]]
);

Expand All @@ -98,7 +100,8 @@ public function testSetMailSettings() {
'mx.nextcloud.org',
'NTLM',
1,
'25'
'25',
null
);
$this->assertSame(Http::STATUS_OK, $response->getStatus());

Expand All @@ -111,7 +114,8 @@ public function testSetMailSettings() {
'mx.nextcloud.org',
'NTLM',
0,
'25'
'25',
null
);
$this->assertSame(Http::STATUS_OK, $response->getStatus());

Expand Down
50 changes: 38 additions & 12 deletions tests/lib/Mail/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,54 @@ public function setUp() {
);
}

public function testGetSendMailInstanceSendMail() {
/**
* @return array
*/
public function sendmailModeProvider(): array {
return [
'smtp' => ['smtp', ' -bs'],
'pipe' => ['pipe', ' -t'],
];
}

/**
* @dataProvider sendmailModeProvider
* @param $sendmailMode
* @param $binaryParam
*/
public function testGetSendmailInstanceSendMail($sendmailMode, $binaryParam) {
$this->config
->expects($this->once())
->expects($this->exactly(2))
->method('getSystemValue')
->with('mail_smtpmode', 'smtp')
->will($this->returnValue('sendmail'));
->will($this->returnValueMap([
['mail_smtpmode', 'smtp', 'sendmail'],
['mail_sendmailmode', 'smtp', $sendmailMode],
]));

$path = \OC_Helper::findBinaryPath('sendmail');
if ($path === null) {
$path = '/usr/sbin/sendmail';
}

$expected = new \Swift_SendmailTransport($path . ' -bs');
$expected = new \Swift_SendmailTransport($path . $binaryParam);
$this->assertEquals($expected, self::invokePrivate($this->mailer, 'getSendMailInstance'));
}

public function testGetSendMailInstanceSendMailQmail() {
/**
* @dataProvider sendmailModeProvider
* @param $sendmailMode
* @param $binaryParam
*/
public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam) {
$this->config
->expects($this->once())
->expects($this->exactly(2))
->method('getSystemValue')
->with('mail_smtpmode', 'smtp')
->will($this->returnValue('qmail'));
->will($this->returnValueMap([
['mail_smtpmode', 'smtp', 'qmail'],
['mail_sendmailmode', 'smtp', $sendmailMode],
]));

$this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
$this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail' . $binaryParam), self::invokePrivate($this->mailer, 'getSendMailInstance'));
}

public function testGetInstanceDefault() {
Expand All @@ -83,8 +107,10 @@ public function testGetInstanceDefault() {
public function testGetInstanceSendmail() {
$this->config
->method('getSystemValue')
->with('mail_smtpmode', 'smtp')
->willReturn('sendmail');
->will($this->returnValueMap([
['mail_smtpmode', 'smtp', 'sendmail'],
['mail_sendmailmode', 'smtp', 'smtp'],
]));

$mailer = self::invokePrivate($this->mailer, 'getInstance');
$this->assertInstanceOf(\Swift_Mailer::class, $mailer);
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/Settings/Admin/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public function testGetForm() {
->method('getSystemValue')
->with('mail_smtppassword', '')
->willReturn('mypassword');
$this->config
->expects($this->at(10))
->method('getSystemValue')
->with('mail_sendmailmode', 'smtp')
->willReturn('smtp');

$expected = new TemplateResponse(
'settings',
Expand All @@ -111,6 +116,7 @@ public function testGetForm() {
'mail_smtpauth' => true,
'mail_smtpname' => 'smtp.sender.com',
'mail_smtppassword' => '********',
'mail_sendmailmode' => 'smtp',
],
''
);
Expand Down