Email authentication issue for outlook using SMTP

Created on 11 February 2025, 2 months ago
Updated 27 February 2025, about 2 months ago

CLIENT -> SERVER: EHLO seaheartsv2.duredemos.com
CLIENT -> SERVER: STARTTLS
CLIENT -> SERVER: EHLO seaheartsv2.duredemos.com
CLIENT -> SERVER: AUTH LOGIN
CLIENT -> SERVER: [credentials hidden]
CLIENT -> SERVER: [credentials hidden]
SMTP ERROR: Password command failed: 535 5.7.139 Authentication unsuccessful, user is locked by your organization's security defaults policy. Contact your administrator. [ZR2P278CA0088.CHEP278.PROD.OUTLOOK.COM 2025-02-11T11:17:15.975Z 08DD481416E3BAE9]
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
Invalid hostentry:
SMTP Error: Could not authenticate

Getting this error if I use Basic authentication for my email functionality using smtp for outlook. This works for Gmail but not outlook. Tried using the SMTP Authentication Support module and also tried creating custom module but still getting the same error.
One solution I found to this was using OAuth 2.0 instead of using the basic authentication. But while using the Azure OAuth 2.0 I am getting error related to the Token not getting generated. But when I tried printing that token then I can see that the token was generated.

Using this code -

<?php

namespace Drupal\custom_content_email\Service;

use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class EmailSenderService {

protected $mailer;
protected $logger;

/**
* Constructor.
*
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
* Logger factory service.
*/
public function __construct(LoggerChannelFactoryInterface $loggerFactory) {
$this->logger = $loggerFactory->get('custom_content_email');

$this->mailer = new PHPMailer(true);

try {
$this->mailer->isSMTP();
$this->mailer->Host = 'smtp.example.com';
$this->mailer->SMTPAuth = true;
$this->mailer->Username = 'your-username@example.com';
$this->mailer->Password = 'your-password';
$this->mailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$this->mailer->Port = 587;

$this->mailer->isHTML(true);
$this->mailer->setFrom('no-reply@example.com', 'SEAHEARTS System');
} catch (Exception $e) {
$this->logger->error('PHPMailer Initialization Error: @error', ['@error' => $e->getMessage()]);
}
}

/**
* Sends an email.
*
* @param string $subject
* Email subject.
* @param string $body
* Email body.
* @param string $to
* Recipient email.
* @param string|null $from
* Sender email (optional).
*
* @return bool
* Returns TRUE if email is sent, FALSE otherwise.
*/
public function sendEmail($subject, $body, $to, $from = NULL) {
if (empty(trim($subject))) {
$this->logger->error('Email subject is empty. Email not sent.');
return FALSE;
}

if (empty($to)) {
$this->logger->error('Recipient email address is empty. Email not sent.');
return FALSE;
}

if ($from) {
$this->mailer->setFrom($from);
}

try {
$this->mailer->Subject = $subject;
$this->mailer->Body = $body;
$this->mailer->addAddress($to);

if ($this->mailer->send()) {
$this->logger->info('Email sent successfully to @to.', ['@to' => $to]);
return TRUE;
} else {
$this->logger->error('Failed to send email to @to with subject @subject.', [
'@to' => $to,
'@subject' => $subject,
]);
return FALSE;
}
} catch (Exception $e) {
$this->logger->error('Email sending error: @error', ['@error' => $e->getMessage()]);
return FALSE;
}
}
}

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024