From 82dd9a01fb0460bc72a9deddc48f285e0846977c Mon Sep 17 00:00:00 2001 From: Mehmet Tolga Avcioglu Date: Fri, 6 Jan 2023 15:59:03 +0300 Subject: [PATCH] improve xclient --- examples/smtp.phps | 5 +++ examples/smtp_xclient.phps | 56 -------------------------------- src/PHPMailer.php | 13 ++++++-- src/SMTP.php | 14 ++++++-- test/PHPMailer/PHPMailerTest.php | 10 ++++++ 5 files changed, 38 insertions(+), 60 deletions(-) delete mode 100644 examples/smtp_xclient.phps diff --git a/examples/smtp.phps b/examples/smtp.phps index f1e07b4f..4a58c2f5 100644 --- a/examples/smtp.phps +++ b/examples/smtp.phps @@ -49,6 +49,11 @@ $mail->AltBody = 'This is a plain-text message body'; //Attach an image file $mail->addAttachment('images/phpmailer_mini.png'); +//SMTP XCLIENT attributes can be passed with setSMTPXclientAttribute method +//$mail->setSMTPXclientAttribute('LOGIN', 'yourname@example.com'); +//$mail->setSMTPXclientAttribute('ADDR', '10.10.10.10'); +//$mail->setSMTPXclientAttribute('HELO', 'test.example.com'); + //send the message, check for errors if (!$mail->send()) { echo 'Mailer Error: ' . $mail->ErrorInfo; diff --git a/examples/smtp_xclient.phps b/examples/smtp_xclient.phps deleted file mode 100644 index e65b49b9..00000000 --- a/examples/smtp_xclient.phps +++ /dev/null @@ -1,56 +0,0 @@ -isSMTP(); -//Enable SMTP debugging -//SMTP::DEBUG_OFF = off (for production use) -//SMTP::DEBUG_CLIENT = client messages -//SMTP::DEBUG_SERVER = client and server messages -$mail->SMTPDebug = SMTP::DEBUG_SERVER; -//Set the hostname of the mail server -$mail->Host = 'mail.example.com'; -//Set the SMTP port number - likely to be 25, 465 or 587 -$mail->Port = 25; -//Whether to use SMTP authentication -$mail->SMTPAuth = false; -$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 -$mail->addReplyTo('replyto@example.com', 'First Last'); -//Set who the message is to be sent to -$mail->addAddress('whoto@example.com', 'John Doe'); -//Set the subject line -$mail->Subject = 'PHPMailer SMTP test'; -//Read an HTML message body from an external file, convert referenced images to embedded, -//convert HTML into a basic plain-text alternative body -$mail->msgHTML(file_get_contents('contents.html'), __DIR__); -//Replace the plain text body with one created manually -$mail->AltBody = 'This is a plain-text message body'; -//Attach an image file -$mail->addAttachment('images/phpmailer_mini.png'); - -//send the message, check for errors -if (!$mail->send()) { - echo 'Mailer Error: ' . $mail->ErrorInfo; -} else { - echo 'Message sent!'; -} diff --git a/src/PHPMailer.php b/src/PHPMailer.php index d8a6cdae..80e1f05b 100644 --- a/src/PHPMailer.php +++ b/src/PHPMailer.php @@ -2006,7 +2006,6 @@ class PHPMailer /** * 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 @@ -2015,7 +2014,7 @@ class PHPMailer */ public function setSMTPXclientAttribute($name, $value) { - if (!in_array($name, ['NAME', 'ADDR', 'PORT', 'PROTO', 'HELO', 'LOGIN', 'DESTADDR', 'DESTPORT'])) { + if (!in_array($name, SMTP::XCLIENT_ATTRIBUTES)) { return false; } if (isset($this->SMTPXClient[$name]) && $value === null) { @@ -2027,6 +2026,16 @@ class PHPMailer return true; } + /** + * Get SMTP XCLIENT attributes + * + * @return array + */ + public function getSMTPXclientAttributes() + { + return $this->SMTPXClient; + } + /** * 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 1a5b4dd8..745b9289 100644 --- a/src/SMTP.php +++ b/src/SMTP.php @@ -71,6 +71,16 @@ class SMTP */ const MAX_REPLY_LENGTH = 512; + /** + * Allowed SMTP XCLIENT attributes. + * Must be allowed by the SMTP server. EHLO response is not checked. + * + * @see https://www.postfix.org/XCLIENT_README.html + * + * @var array + */ + const XCLIENT_ATTRIBUTES = ['NAME', 'ADDR', 'PORT', 'PROTO', 'HELO', 'LOGIN', 'DESTADDR', 'DESTPORT']; + /** * Debug level for no output. * @@ -965,14 +975,14 @@ class SMTP /** * Send SMTP XCLIENT command to server and check its return code. - * Possible keys NAME, ADDR, PORT, PROTO, HELO, LOGIN, DESTADDR, DESTPORT + * * @return bool True on success */ public function xclient(array $vars) { $xclient_options = ""; foreach ($vars as $key => $value) { - if (in_array($key, ['NAME', 'ADDR', 'PORT', 'PROTO', 'HELO', 'LOGIN', 'DESTADDR', 'DESTPORT'])) { + if (in_array($key, SMTP::XCLIENT_ATTRIBUTES)) { $xclient_options .= " {$key}={$value}"; } } diff --git a/test/PHPMailer/PHPMailerTest.php b/test/PHPMailer/PHPMailerTest.php index a17e37a2..56661272 100644 --- a/test/PHPMailer/PHPMailerTest.php +++ b/test/PHPMailer/PHPMailerTest.php @@ -1200,7 +1200,17 @@ EOT; $this->Mail->SMTPAuth = false; $this->Mail->setSMTPXclientAttribute('ADDR', '127.0.0.1'); $this->Mail->setSMTPXclientAttribute('LOGIN', 'user@example.com'); + $this->Mail->setSMTPXclientAttribute('HELO', 'test.example.com'); $this->assertFalse($this->Mail->setSMTPXclientAttribute('INVALID', 'value')); + + $attributes = $this->Mail->getSMTPXclientAttributes(); + $this->assertEquals('test.example.com', $attributes['HELO']); + + // remove attribute + $this->Mail->setSMTPXclientAttribute('HELO', null); + $attributes = $this->Mail->getSMTPXclientAttributes(); + $this->assertEquals(['ADDR' => '127.0.0.1', 'LOGIN' => 'user@example.com'], $attributes); + $this->Mail->Subject .= ': Testing XCLIENT'; $this->buildBody(); $this->Mail->clearAllRecipients();