php-swiftmailerHow to send emails with Swiftmailer?
Sending emails with Swiftmailer is easy and straightforward.
// 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);
// 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 output of the example code is $result
which is an integer representing the number of successful recipients.
Code explanation
$transport
: This is the transport object which is responsible for sending the message. It requires the SMTP server address and port.$mailer
: This is the mailer object which is responsible for sending the message using the transport object.$message
: This is the message object which contains the message details such as sender, recipient, subject and body.$result
: This is the result of the send operation which is an integer representing the number of successful recipients.
Helpful links
More of Php Swiftmailer
- How to use SMTP with Swiftmailer?
- How to set timeout with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to configure Swiftmailer for SMTP without authentication?
- How to send emails in UTF8 using Swiftmailer?
- How to get the error message in Swiftmailer?
- How to use Swiftmailer transport?
- How to use Swiftmailer with Symfony?
- How to use Swiftmailer to send RFC 2822 compliant emails?
See more codes...