如何使用 phpmailer 向收件人发送具有唯一 ID 的退订 link
how to send an unsubscribe link with an unique id to the recipients with phpmailer
我正在使用 phpmailer 向一些订阅者发送时事通讯。每个订阅者都是 "Category" 的一部分。在发送之前,我首先选择订阅者所属的类别。每个用户都有一个名为 $recipients_id
的唯一 ID。
发生了什么:在下面的代码中,每个用户都获得了所有 "unscribe links";其他成员也是如此。 he/she 应该只接收退订 his/her 自己的 id:
// part of the script
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = "Newsletter"; //Sets the Subject of the message
$mail->Body = $_POST["message"];
foreach($category_matches as $file) { // grab subscribers of the category
// get data out of txt file
$lines = file($file, FILE_IGNORE_NEW_LINES); // set lines from matched file into an array
$recipients_id = $lines[0]; // id of recipients
$recipients_name = $lines[2]; // name of recipients
$recipients_email = $lines[3]; // email of the recipients
$mail->AddBCC($recipients_email, $recipients_name); //bcc to all subscribers of the category
$mail->Body .= '<a href="http://example.com/newsletter/unsubscribe.php?id='.$recipients_id.'">Unsubscribe</a>'; //unsubscribe anchor
echo $recipients_id.'<br />'; // this echos me the id's of the subscribers in the category to check only
}
if($mail->Send()) //Send an Email. Return true on success or false on error
{
$result = '<div class="alert alert-success">Newsletter sent to subscribers of:<b> '.$recipients_category.'</b></div>';
}
else
{
$result = 'div class="alert alert-danger">There is an Error</div>';
}
根据上面的评论,你的代码应该是这样的:
// part of the script
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = "Newsletter"; //Sets the Subject of the message
foreach($category_matches as $file) { // grab subscribers of the category
// get data out of txt file
$lines = file($file, FILE_IGNORE_NEW_LINES); // set lines from matched file into an array
$recipients_id = $lines[0]; // name of recipients
$recipients_name = $lines[2]; // name of recipients
$recipients_email = $lines[3]; // email of the recipients
$mail->AddAddress($recipients_email, $recipients_name); //Adds a "To" address
try {
$mail->Body = $_POST["message"].'<br /><br />'.'<a href="http://example.com/newsletter/unsubscribe.php?id='.$recipients_id.'">Unsubscribe</a>'; //unsubscribe anchor
$mail->Send();
$result = '<div class="alert alert-success">Newsletter sent to subscribers of:<b> '.$recipients_category.'</b></div>';
} catch (Exception $e) {
$result = '<div class="alert alert-success">Mailer Error (' . htmlspecialchars($recipients_email) . ') ' . $mail->ErrorInfo . '</div>';
$mail->smtp->reset(); // reset SMTP connection
}
$mail->clearAddresses(); // Clear all addresses for the next iteration
}
注意:只有 1 个 $mail->Body
,其中包括消息和用于退订的 unique_id link。
将 echo 绑定到 var $result
并在循环外 echo $result
;否则,对于您发送的每个收件人,您将多次收到它们
我正在使用 phpmailer 向一些订阅者发送时事通讯。每个订阅者都是 "Category" 的一部分。在发送之前,我首先选择订阅者所属的类别。每个用户都有一个名为 $recipients_id
的唯一 ID。
发生了什么:在下面的代码中,每个用户都获得了所有 "unscribe links";其他成员也是如此。 he/she 应该只接收退订 his/her 自己的 id:
// part of the script
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = "Newsletter"; //Sets the Subject of the message
$mail->Body = $_POST["message"];
foreach($category_matches as $file) { // grab subscribers of the category
// get data out of txt file
$lines = file($file, FILE_IGNORE_NEW_LINES); // set lines from matched file into an array
$recipients_id = $lines[0]; // id of recipients
$recipients_name = $lines[2]; // name of recipients
$recipients_email = $lines[3]; // email of the recipients
$mail->AddBCC($recipients_email, $recipients_name); //bcc to all subscribers of the category
$mail->Body .= '<a href="http://example.com/newsletter/unsubscribe.php?id='.$recipients_id.'">Unsubscribe</a>'; //unsubscribe anchor
echo $recipients_id.'<br />'; // this echos me the id's of the subscribers in the category to check only
}
if($mail->Send()) //Send an Email. Return true on success or false on error
{
$result = '<div class="alert alert-success">Newsletter sent to subscribers of:<b> '.$recipients_category.'</b></div>';
}
else
{
$result = 'div class="alert alert-danger">There is an Error</div>';
}
根据上面的评论,你的代码应该是这样的:
// part of the script
$mail->IsHTML(true); //Sets message type to HTML
$mail->Subject = "Newsletter"; //Sets the Subject of the message
foreach($category_matches as $file) { // grab subscribers of the category
// get data out of txt file
$lines = file($file, FILE_IGNORE_NEW_LINES); // set lines from matched file into an array
$recipients_id = $lines[0]; // name of recipients
$recipients_name = $lines[2]; // name of recipients
$recipients_email = $lines[3]; // email of the recipients
$mail->AddAddress($recipients_email, $recipients_name); //Adds a "To" address
try {
$mail->Body = $_POST["message"].'<br /><br />'.'<a href="http://example.com/newsletter/unsubscribe.php?id='.$recipients_id.'">Unsubscribe</a>'; //unsubscribe anchor
$mail->Send();
$result = '<div class="alert alert-success">Newsletter sent to subscribers of:<b> '.$recipients_category.'</b></div>';
} catch (Exception $e) {
$result = '<div class="alert alert-success">Mailer Error (' . htmlspecialchars($recipients_email) . ') ' . $mail->ErrorInfo . '</div>';
$mail->smtp->reset(); // reset SMTP connection
}
$mail->clearAddresses(); // Clear all addresses for the next iteration
}
注意:只有 1 个 $mail->Body
,其中包括消息和用于退订的 unique_id link。
将 echo 绑定到 var $result
并在循环外 echo $result
;否则,对于您发送的每个收件人,您将多次收到它们