php-swiftmailerHow to get the response code when using Swiftmailer?
Swiftmailer provides a way to get the response code when sending an email. The getResponse()
method of the Swift_Transport_AbstractSmtpTransport
class can be used to get the response code.
Example code
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25);
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Test Subject');
$mailer->send($message);
$responseCode = $transport->getResponse();
Output example
250 OK
Code explanation
Swift_SmtpTransport::newInstance('smtp.example.org', 25)
: creates a new instance of theSwift_SmtpTransport
class, passing in the SMTP server address and port number.Swift_Mailer::newInstance($transport)
: creates a new instance of theSwift_Mailer
class, passing in the$transport
object.Swift_Message::newInstance('Test Subject')
: creates a new instance of theSwift_Message
class, passing in the subject of the email.$mailer->send($message)
: sends the email using the$mailer
object, passing in the$message
object.$transport->getResponse()
: gets the response code from the$transport
object.
Helpful links
More of Php Swiftmailer
- How to set timeout with Swiftmailer?
- How to use SMTP with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to use Swiftmailer to send RFC 2822 compliant emails?
- How to configure Swiftmailer for Postfix?
- How to send emails in UTF8 using Swiftmailer?
- How to send emails to multiple recipients with Swiftmailer?
- How to set the port for Swiftmailer?
- How to enable TLS with Swiftmailer?
See more codes...