This is my mailing script, i m using for loop to send multiple emails.
if(isset($_POST['submit'])){
require ("vendor/autoload.php");
$fr_email = $_POST['fr_email'];
$from = $_POST['from'];
$reply_to = $_POST['reply_to'];
$sub = $_POST['sub'];
$tm = $_POST['email'];
$str = "<117418239>
<128422057>";
$listid= explode("\n",$str);
$mail = new PHPMailer\PHPMailer\PHPMailer;
for ($x = 2; $x > 0 ; $x--){
$mail->isSMTP();
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
));
$mail->SMTPDebug = 0;
$mail->Host = 'hostname';
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->Username = 'uname';
$mail->Password = 'pwd';
$mail->SMTPSecure = 'tls';
$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';
$mail->isHTML(true);
$mail->setFrom($fr_email,$from);
$mail->AddReplyTo($reply_to,$from);
$mail->Sender=$fr_email;
$mail->Subject=$sub;
$mail->XMailer = ' ';
$mail->addCustomHeader('List-ID', $listid[$x-1]);
$body = $_POST['body'];
$mail->Body=$body;
//$mail->AltBody=$altbody;
$mail->addAddress($tm);
if ($mail->send()) {
echo "\nMessage sent\n";
}
else {
echo 'Mailer error: ' . $mail->ErrorInfo;
}
$mail->clearAddresses();
$mail->SmtpClose();
} // for loop close
}
I'm using "addcustomheader" function to add list id header field. Bcoz i m running a loop i m getting multiple entries of list id field in my email header. [even though i m closing my smtp connection after each iteration]
for 1st email
List-ID: <117418239>
for 2nd email
List-ID: <117418239>
List-ID: <128422057>
Is there a way to clear the custom headers after each iteration of the loop, like clearing the email address
Yes. Note the name of the method addCustomHeader
– it adds a header, and does not replace existing ones. To fix this, call clearCustomHeaders()
after sending within your loop. See the docs on this method.