From 6384753f6e02da27ec255f4cb9cadb061d8b5aeb Mon Sep 17 00:00:00 2001 From: Mehmet Tolga Avcioglu Date: Wed, 4 Jan 2023 14:13:07 +0300 Subject: [PATCH] protected xclient variable and added tests --- .../{smtp-xclient.phps => smtp_xclient.phps} | 4 ++- src/PHPMailer.php | 28 +++++++++++++++++-- src/SMTP.php | 2 +- test/PHPMailer/PHPMailerTest.php | 19 +++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) rename examples/{smtp-xclient.phps => smtp_xclient.phps} (91%) diff --git a/examples/smtp-xclient.phps b/examples/smtp_xclient.phps similarity index 91% rename from examples/smtp-xclient.phps rename to examples/smtp_xclient.phps index abd76324..e65b49b9 100644 --- a/examples/smtp-xclient.phps +++ b/examples/smtp_xclient.phps @@ -29,7 +29,9 @@ $mail->Host = 'mail.example.com'; $mail->Port = 25; //Whether to use SMTP authentication $mail->SMTPAuth = false; -$mail->SMTPXClient = ['LOGIN' => 'yourname@example.com']; +$mail->setSMTPXclientAttribute('LOGIN', 'yourname@example.com'); +$mail->setSMTPXclientAttribute('ADDR', '10.10.10.10'); +$mail->setSMTPXclientAttribute('HELO', 'test.example.com'); //Set who the message is to be sent from $mail->setFrom('from@example.com', 'First Last'); //Set an alternative reply-to address diff --git a/src/PHPMailer.php b/src/PHPMailer.php index 8ab4c024..4145e968 100644 --- a/src/PHPMailer.php +++ b/src/PHPMailer.php @@ -358,12 +358,11 @@ class PHPMailer public $AuthType = ''; /** - * SMTP SMTPXClient command variables - * Options are NAME | ADDR | PORT | PROTO | HELO | LOGIN | DESTADDR | DESTPORT + * SMTP SMTPXClient command attibutes * * @var array */ - public $SMTPXClient = []; + protected $SMTPXClient = []; /** * An implementation of the PHPMailer OAuthTokenProvider interface. @@ -2005,6 +2004,29 @@ class PHPMailer return $this->smtp; } + /** + * Provide SMTP XCLIENT attributes + * Possible attributes are NAME, ADDR, PORT, PROTO, HELO, LOGIN, DESTADDR, DESTPORT + * + * @param string $name Attribute name + * @param ?string $value Attribute value + * + * @return bool + */ + public function setSMTPXclientAttribute($name, $value) + { + if (!in_array($name, ['NAME', 'ADDR', 'PORT', 'PROTO', 'HELO', 'LOGIN', 'DESTADDR', 'DESTPORT'])) { + return false; + } + if (isset($this->SMTPXClient[$name]) && $value === null) { + unset($this->SMTPXClient[$name]); + } else if ($value !== null) { + $this->SMTPXClient[$name] = $value; + } + + return true; + } + /** * Send mail via SMTP. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input. diff --git a/src/SMTP.php b/src/SMTP.php index 67c77704..1a5b4dd8 100644 --- a/src/SMTP.php +++ b/src/SMTP.php @@ -965,7 +965,7 @@ class SMTP /** * Send SMTP XCLIENT command to server and check its return code. - * Possible keys NAME | ADDR | PORT | PROTO | HELO | LOGIN | DESTADDR | DESTPORT + * Possible keys NAME, ADDR, PORT, PROTO, HELO, LOGIN, DESTADDR, DESTPORT * @return bool True on success */ public function xclient(array $vars) diff --git a/test/PHPMailer/PHPMailerTest.php b/test/PHPMailer/PHPMailerTest.php index fc3f61bb..a17e37a2 100644 --- a/test/PHPMailer/PHPMailerTest.php +++ b/test/PHPMailer/PHPMailerTest.php @@ -1191,6 +1191,25 @@ EOT; ); } + /** + * Test SMTP Xclient options + */ + public function testSmtpXclient() + { + $this->Mail->isSMTP(); + $this->Mail->SMTPAuth = false; + $this->Mail->setSMTPXclientAttribute('ADDR', '127.0.0.1'); + $this->Mail->setSMTPXclientAttribute('LOGIN', 'user@example.com'); + $this->assertFalse($this->Mail->setSMTPXclientAttribute('INVALID', 'value')); + $this->Mail->Subject .= ': Testing XCLIENT'; + $this->buildBody(); + $this->Mail->clearAllRecipients(); + self::assertTrue($this->Mail->addAddress('a@example.com'), 'Addressing failed'); + $this->Mail->preSend(); + self::assertTrue($this->Mail->send(), 'send failed'); + } + + /** * Test SMTP host connections. * This test can take a long time, so run it last.