From 63fed3c6c7216bbb2a365f17dfb193e8ccc09066 Mon Sep 17 00:00:00 2001 From: Redjard Date: Sun, 13 Jul 2025 05:50:34 +0200 Subject: [PATCH 1/2] implement AUTH PLAIN AUTH LOGIN is no longer the default from the looks, AUTH PLAIN appears to be more common. I ran into a server that did not support AUTH LOGIN at all, so I implemented AUTH PLAIN as an optional toggle. The default behavior remains unchanged, calling `setLogin('', '', true)` instead of `setLogin('', '')` will use AUTH PLAIN. --- src/Email.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Email.php b/src/Email.php index 3bdb015..2990d69 100644 --- a/src/Email.php +++ b/src/Email.php @@ -33,6 +33,9 @@ class Email /** @var string $password */ protected $password; + /** @var bool $plain_auth */ + protected $plain_auth; + /** @var int $connectionTimeout */ protected $connectionTimeout; @@ -195,10 +198,11 @@ public function addAttachment($attachment) * @param string $password * @return Email */ - public function setLogin($username, $password) + public function setLogin($username, $password, $plain_auth = false) { $this->username = $username; $this->password = $password; + $this->plain_auth = $plain_auth; return $this; } @@ -327,9 +331,15 @@ public function send() $this->logs['HELLO'][2] = $this->sendCommand('EHLO ' . $this->hostname); } - $this->logs['AUTH'] = $this->sendCommand('AUTH LOGIN'); - $this->logs['USERNAME'] = $this->sendCommand(base64_encode($this->username)); - $this->logs['PASSWORD'] = $this->sendCommand(base64_encode($this->password)); + if ($this->plain_auth) { + $this->logs['AUTH'] = $this->sendCommand('AUTH PLAIN'); + $this->logs['AUTH_PLAIN'] = $this->sendCommand(base64_encode( "\0". $this->username ."\0". $this->password )); + } else { + $this->logs['AUTH'] = $this->sendCommand('AUTH LOGIN'); + $this->logs['USERNAME'] = $this->sendCommand(base64_encode($this->username)); + $this->logs['PASSWORD'] = $this->sendCommand(base64_encode($this->password)); + } + $this->logs['MAIL_FROM'] = $this->sendCommand('MAIL FROM: <' . $this->from[0] . '>'); $recipients = array_merge($this->to, $this->cc, $this->bcc); From 78c03b62e240941838bb98e1009b61a3494b60a2 Mon Sep 17 00:00:00 2001 From: Redjard Date: Sun, 13 Jul 2025 05:53:25 +0200 Subject: [PATCH 2/2] documentation for using AUTH PLAIN --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c7fae07..0c66212 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ composer require snipworks/php-smtp use Snipworks\Smtp\Email; $mail = new Email('smtp.example.com', 25); -$mail->setLogin('sender@example.com', 'password'); +$mail->setLogin('sender@example.com', 'password', false); // set 3rd parameter to true to use PLAIN auth instead of LOGIN auth $mail->addTo('recipient@example.com', 'Example Receiver'); $mail->setFrom('example@example.com', 'Example Sender'); $mail->setSubject('Example subject'); @@ -38,7 +38,7 @@ use Snipworks\Smtp\Email; $mail = new Email('smtp.example.com', 587); $mail->setProtocol(Email::TLS); -$mail->setLogin('sender@example.com', 'password'); +$mail->setLogin('sender@example.com', 'password', false); // set 3rd parameter to true to use PLAIN auth instead of LOGIN auth $mail->addTo('recipient@example.com', 'Example Receiver'); $mail->setFrom('example@example.com', 'Example Sender'); $mail->setSubject('Example subject'); @@ -67,7 +67,7 @@ define('SMTP_PRIMARY_PASSWORD', 'my very secret password'); require_once('config.php'); // ... -$mail->setLogin(SMTP_PRIMARY_EMAIL, SMTP_PRIMARY_PASSWORD); +$mail->setLogin(SMTP_PRIMARY_EMAIL, SMTP_PRIMARY_PASSWORD, false); // ... ```