Ask Your Question
2

How can PHP mailer be used to send an email to several recipients at once in PHP?

asked 2022-04-14 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-01-16 14:00:00 +0000

ladyg gravatar image

To send an email to several recipients at once using PHP mailer, you can use the following steps:

  1. Import the PHPMailer library using the code below:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php'; // This will load the PHPMailer library
  1. Instantiate the PHPMailer class:
$mail = new PHPMailer(true);
  1. Set the SMTP access details:
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // This can be changed to any SMTP host
$mail->SMTPAuth = true;
$mail->Username = 'youremail@gmail.com'; // Your email address
$mail->Password = 'yourpassword'; // Your email password
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
  1. Set the email details, such as the sender, recipient, subject, and content:
$mail->setFrom('youremail@gmail.com', 'Your Name');
$mail->addAddress('recipient1@example.com', 'Recipient 1');
$mail->addAddress('recipient2@example.com', 'Recipient 2');
$mail->Subject = 'Email Subject';
$mail->Body = 'Email content goes here.';
  1. Use the send() method to send the email:
if ($mail->send()) {
  echo 'Email sent successfully.';
} else {
  echo 'Unable to send email. Error: ' . $mail->ErrorInfo;
}

By adding multiple recipients with the addAddress() method, you can send the same email to several recipients at once.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-14 11:00:00 +0000

Seen: 10 times

Last updated: Jan 16 '22