How to get smtp error when mail send in php
To get SMTP errors when sending an email using PHP, you can use the error_reporting
and ini_set
functions to enable error reporting and set the error level to E_ALL
. You can also use the smtp_error
parameter in the mail
function to enable SMTP error reporting.
Here is an example:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$to = '[email protected]';
$subject = 'Test email';
$message = 'This is a test email';
$headers = array(
'From' => '[email protected]',
'Reply-To' => '[email protected]',
'Content-Type' => 'text/plain; charset=UTF-8'
);
$smtp = 'smtp.example.com';
$smtp_port = 587;
$username = 'smtp_username';
$password = 'smtp_password';
$mail = mail($to, $subject, $message, implode("\r\n", $headers), '-f '. $username. ' '. $password. ' '. $smtp. ' '. $smtp_port);
if (!$mail) {
echo 'Error sending email: '. error_get_last()['message'];
}
?>
In this example, the error_reporting
function is used to enable error reporting, and the ini_set
function is used to set the error level to E_ALL
. The smtp_error
parameter is set to true
to enable SMTP error reporting.
When you run this script, if there is an error sending the email, the error message will be displayed.
You can also use the PEAR Mail
class to send emails and get SMTP errors. Here is an example:
<?php
require_once 'Mail.php';
$from = '[email protected]';
$to = '[email protected]';
$subject = 'Test email';
$body = 'This is a test email';
$headers = array(
'From' => $from,
'Reply-To' => $from,
'Content-Type' => 'text/plain; charset=UTF-8'
);
$smtp = 'smtp.example.com';
$smtp_port = 587;
$username = 'smtp_username';
$password = 'smtp_password';
$mail = Mail::factory('smtp', array(
'host' => $smtp,
'port' => $smtp_port,
'auth' => true,
'username' => $username,
'password' => $password
));
$mail->setFrom($from);
$mail->addAddress($to);
$mail->Subject = $subject;
$mail->Body = $body;
if (!$mail->send()) {
echo 'Error sending email: '. $mail->ErrorInfo;
}
?>
In this example, the PEAR Mail
class is used to send the email. The smtp_error
parameter is set to true
to enable SMTP error reporting. If there is an error sending the email, the error message will be displayed.
You can also use the SwiftMailer
library to send emails and get SMTP errors. Here is an example:
<?php
require_once 'Swift.php';
$from = '[email protected]';
$to = '[email protected]';
$subject = 'Test email';
$body = 'This is a test email';
$headers = array(
'From' => $from,
'Reply-To' => $from,
'Content-Type' => 'text/plain; charset=UTF-8'
);
$smtp = 'smtp.example.com';
$smtp_port = 587;
$username = 'smtp_username';
$password = 'smtp_password';
$mailer = Swift_Mailer::newInstance(Swift_SmtpTransport::newInstance($smtp, $smtp_port, $username, $password));
$message = Swift_Message::newInstance($subject, $body);
$message->setFrom($from);
$message->addAddress($to);
if (!$mailer->send($message)) {
echo 'Error sending email: '. $mailer->getException()->getMessage();
}
?>
In this example, the SwiftMailer
library is used to send the email. The smtp_error
parameter is set to true
to enable SMTP error reporting. If there is an error sending the email, the error message will be displayed.
Note that the above examples are just a few ways to get SMTP errors when sending an email using PHP. The specific method you use will depend on your PHP version and the email library you are using.