PHPMailer Multiple Recipient Example

Below is an example of using PHPMailer.

<?php
                            
                            // phpmailer--multi-recipient-example.php
                            // Inject multiple test messages into Kasplo Engine using PHPMailer 
                            
                            // Use PHPMailer
                            require_once('/usr/share/php/PHPMailer/class.phpmailer.php');
                            require_once('/usr/share/php/PHPMailer/class.smtp.php');
                            
                            // Kasplo Engine installation settings
                            $mail->Host = "mta.example.com"; // Connect to this Kasplo server
                            $mail->SMTPAuth = true; // enables SMTP authentication. Set to false for IP-based authentication
                            $mail->Port = 587; // SMTP submission port to inject mail into. Usually port 587 or 25
                            $mail->Username = "smtpauth@example.com"; // SMTP username
                            $mail->Password = "password"; // SMTP password
                            //$mail->SMTPDebug = 2; // uncomment to print debugging info
                            
                            // Timezone
                            date_default_timezone_set('America/Chicago');
                            
                            // Campaign Settings
                            $mail_class = "transactional"; // Mail Class to use
                            $from_address = "test@example.com";
                            $from_name = "Newsletter";
                            $subject = " PHPMailer Example";
                            $html_body = "HTML body goes here.";
                            $text_body = "Text body goes here.";
                            
                            // List of recipients
                            $recipients = array(
                                "address1@discardallmail.drh.net" => "Name 1",
                                "address2@discardallmail.drh.net" => "Name 2",
                            );
                            
                            // Create the SMTP session
                            $mail = new PHPMailer();
                            $mail->IsSMTP(); // Use SMTP
                            $mail->SMTPKeepAlive = true; // prevent the SMTP session from being closed after each message
                            $mail->SmtpConnect();
                            
                            // Set headers that are constant for every message outside of the foreach loop
                            $mail->SetFrom($from_address, $from_name);
                            $mail->Subject = $subject;
                            $mail->addCustomHeader("X-Kasplo-MailClass: $mail_class");
                            
                            // Send a message to each recipient.
                            // Headers that are unique for each message should be set within the foreach loop
                            foreach ($recipients as $email => $name) {
                            
                                // Generate headers that are unique for each message
                            	$mail->ClearAllRecipients();
                                $mail->AddAddress($email, $name);
                            
                                // Generate the message
                            	$mail->MsgHTML($html_body);
                                $mail->AltBody = $text_body;
                            
                                // Send the message 
                            	if($mail->Send()) {
                                    echo "Message sent!\n";
                                } else {
                                    echo "Mailer Error: " . $mail->ErrorInfo . "\n";
                                }
                            
                            }
                            
                            // Close the SMTP session
                            $mail->SmtpClose();