php-swiftmailerHow to send emails to multiple recipients with Swiftmailer?
Swiftmailer is a popular library for sending emails in PHP. It can be used to send emails to multiple recipients in a few simple steps.
<?php
// 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);
echo $result;
Output example
2
The code above will send an email to two recipients. It starts by creating a transport object with the SMTP server details. Then a mailer object is created using the transport object. Finally, a message object is created with the sender and recipient details, and the message body. The message is then sent using the mailer object, and the result is echoed.
Parts of the code:
$transport
: This is the transport object which contains the SMTP server details.$mailer
: This is the mailer object which is used to send the message.$message
: This is the message object which contains the sender and recipient details, and the message body.$result
: This is the result of sending the message.
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 set the 'from' address in Swiftmailer?
- How to configure Swiftmailer for SMTP without authentication?
- How to use Swiftmailer with Symfony?
- How to use Swiftmailer to send RFC 2822 compliant emails?
- How to send emails in UTF8 using Swiftmailer?
- How to use Swiftmailer transport?
- How to set the sender name in SwiftMailer?
See more codes...