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_Messageclass.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_Messageobject.getId(): returns the unique ID of the sent email.
Helpful links
More of Php Swiftmailer
- How to use SMTP with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to configure Swiftmailer for SMTP without authentication?
- How to set timeout with Swiftmailer?
- How to send multiple attachments with SwiftMailer?
- How to get the message ID in SwiftMailer?
- How to send emails in UTF8 using Swiftmailer?
- How to use Swiftmailer with SendGrid?
- How to set the port for Swiftmailer?
- How to configure Swiftmailer logger?
See more codes...