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); // ... ``` 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);