improve xclient

This commit is contained in:
Mehmet Tolga Avcioglu 2023-01-06 15:59:03 +03:00
parent b278bc3f1b
commit 82dd9a01fb
5 changed files with 38 additions and 60 deletions

View File

@ -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;

View File

@ -1,56 +0,0 @@
<?php
/**
* This example shows making an SMTP connection with authentication.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require '../vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->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!';
}

View File

@ -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.

View File

@ -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}";
}
}

View File

@ -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();