9951 explained code solutions for 126 technologies


php-swiftmailerHow to get the error message in Swiftmailer?


Swiftmailer is a popular library for sending emails in PHP. To get the error message in Swiftmailer, you can use the getErrors() method. This method will return an array of errors that occurred while sending the email.

Example code

<?php
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25);
$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Test Subject')
  ->setFrom(array('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]', '[email protected]' => 'A name'))
  ->setBody('Here is the message itself');

try {
  $result = $mailer->send($message);
} catch (Exception $e) {
  $errors = $mailer->getErrors();
  print_r($errors);
}

Output example

Array
(
    [0] => Failed to authenticate on SMTP server with username "[email protected]"
    [1] => SMTP server did not accept the password
)

The getErrors() method will return an array of errors that occurred while sending the email. The array will contain a description of the error that occurred. This can be used to debug any issues that may arise while sending emails.

Helpful links

Edit this code on GitHub