phpmailer - The following SMTP Error: Data not accepted
The "SMTP Error: Data not accepted" is a common error message that can occur when using the PHPMailer library to send email messages.
The "SMTP Error: Data not accepted" is a common error message that can occur when using the PHPMailer library to send email messages. This error typically occurs when the SMTP server you are using to send the email message is unable to accept the data you are sending. This can be caused by a number of different issues, such as incorrect SMTP login credentials, a failure to connect to the SMTP server, or an issue with the email message itself. To resolve this error, you will need to troubleshoot the specific cause of the issue and make the necessary adjustments to your code or configuration.
Troubleshooting Steps
- Enable SMTP Debugging: Set
$mail->SMTPDebug = 2;to view the exact server response and identify where the connection fails. - Verify SMTP Credentials & Connection: Double-check the host, port, encryption method (
STARTTLSorSSL), username, and password. Ensure your server IP isn't blocked by the provider. - Check Email Size & Content Limits: SMTP servers often reject messages exceeding size limits (commonly 10–25 MB) or containing malformed headers, invalid characters, or excessive attachments.
- Validate Headers & Recipients: Ensure the
Fromaddress matches the authenticated SMTP account, and avoid line breaks or special characters in subject/body fields that could trigger header injection filters.
PHPMailer Configuration & Error Handling Example
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
// SMTP Configuration
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Enable debugging for troubleshooting
$mail->SMTPDebug = 2;
// Recipients & Content
$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test Email';
$mail->Body = '<p>This is a test message.</p>';
$mail->send();
echo 'Message sent successfully';
} catch (Exception $e) {
echo "SMTP Error: Data not accepted. Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>