php-swiftmailerHow to use Swiftmailer transport?
Swiftmailer transport is a library used to send emails from PHP applications. It is easy to use and provides a wide range of features.
To use Swiftmailer transport, you need to create a Swift_Transport
instance and pass it to the Swift_Mailer
constructor.
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.example.org', 25))
->setUsername('your username')
->setPassword('your password')
;
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
The code above creates a Swift_SmtpTransport
instance and passes it to the Swift_Mailer
constructor. The Swift_SmtpTransport
class is used to send emails via SMTP.
To send an email, you need to create a Swift_Message
instance and pass it to the send()
method of the Swift_Mailer
instance.
// Create a message
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['[email protected]' => 'John Doe'])
->setTo(['[email protected]', '[email protected]' => 'A name'])
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
The code above creates a Swift_Message
instance and passes it to the send()
method of the Swift_Mailer
instance. The send()
method returns the number of successful recipients.
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...