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_SmtpTransportclass, passing in the SMTP server address and port number.Swift_Mailer::newInstance($transport): creates a new instance of theSwift_Mailerclass, passing in the$transportobject.Swift_Message::newInstance('Test Subject'): creates a new instance of theSwift_Messageclass, passing in the subject of the email.$mailer->send($message): sends the email using the$mailerobject, passing in the$messageobject.$transport->getResponse(): gets the response code from the$transportobject.
Helpful links
More of Php Swiftmailer
- How to configure Swiftmailer for SMTP without authentication?
- How to use SMTP with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to set timeout with Swiftmailer?
- How to set the reply to address in Swiftmailer?
- How to set the port for Swiftmailer?
- How to set the sender name in SwiftMailer?
- How to send multiple attachments with SwiftMailer?
- How to encrypt emails with SwiftMailer?
See more codes...