php-swiftmailerHow to check if an email was sent using Swiftmailer?
Swiftmailer provides a way to check if an email was sent using its send()
method. The send()
method returns a Swift_Message
object which contains a getId()
method that can be used to check if the email was sent.
Example code
$message = Swift_Message::newInstance()
->setSubject('Test Email')
->setFrom('[email protected]')
->setTo('[email protected]')
->setBody('This is a test email.');
$sent = $mailer->send($message);
if ($sent) {
echo $message->getId();
}
Output example
<[email protected]>
Code explanation
Swift_Message::newInstance()
: creates a new instance of theSwift_Message
class.setSubject()
: sets the subject of the email.setFrom()
: sets the sender of the email.setTo()
: sets the recipient of the email.setBody()
: sets the body of the email.send()
: sends the email and returns aSwift_Message
object.getId()
: returns the unique ID of the sent email.
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 get the response code when using 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...