如何在电子邮件中生成取消订阅链接


How to generate unsubscribe link in email?

我有一个时事通讯系统,所以当我想向所有订阅者发送消息时,我通过一个名为 post-processing.php 的 php 文件发送它。

这是我的post-processing.php代码:

$count=$dbo->prepare("select * from sw_post where post_id=:post_id");
$count->bindParam(":post_id",$post_id,PDO::PARAM_INT,4);
if($count->execute()){
   echo " Success <br>";
   $row = $count->fetch(PDO::FETCH_OBJ);
   $sub=$row->sub;
   $post=$row->post;
} else {
   echo "Database Error ..";
print_r($count->errorInfo()); 
exit;
}
/////////////////////////
$dtl="";
$dtl .=$post. "'n'n";

@$headers.="Reply-to: $from_email'n"; 
$headers .= "From: $from_email'n"; 
$headers .= "Errors-to: $from_email'n"; 
//$headers = "Content-Type: text/html; charset=iso-8859-1'n".$headers;
// Above line is required to send HTML email 
$sql="select email_id, email from sw_email where status='A'"; 
$i=0;
foreach ($dbo->query($sql) as $row) {
   $url=$base_url."unsubscribe.php?email_id=$row[email_id]&email=$row[email]&todo=delete";
   $dtl .= "Click on the URL to unsubscribe  $url ";

假设我有三个订阅者,当我收到电子邮件时,我得到以下结果:

Click on the URL to unsubscribe: http://#.com/unsubscribe.php?email_id=1&email=example1@email.com&todo=delete
Click on the URL to unsubscribe: http://#.com/unsubscribe.php?email_id=2&email=example2@email.com&todo=delete
Click on the URL to unsubscribe: http://#.com/unsubscribe.php?email_id=3&email=example3@email.com&todo=delete

但我想要的结果只是:

Click on the URL to unsubscribe: http://#.com/unsubscribe.php?email_id=1&email=example@email.com&todo=delete

不希望收款人收到取消订阅我所有订阅者的链接。我该如何解决这个问题?

获取用户列表后,将您的电子邮件代码绑定到 foreach 中

  @$headers.="Reply-to: $from_email'n"; 
  $headers .= "From: $from_email'n"; 
  $headers .= "Errors-to: $from_email'n"; 
//$headers = "Content-Type: text/html; charset=iso-8859-1'n".$headers;
$sql="select email_id, email from sw_email where status='A'"; 
foreach ($dbo->query($sql) as $row) {
    $dtl =$post. "'n'n";    
    $url=$base_url."unsubscribe.php?email_id=$row[email_id]&email=$row[email]&todo=delete";
    $dtl .= "Click on the URL to unsubscribe  $url ";
    mail('to_email','subject',$dtl,$headers); //this is your mail function
}

您需要做的是将消息正文保留在订阅者循环之外的变量中不变。然后,逐个读取其电子邮件地址,将消息和链接附加到新变量。仅使用该电子邮件地址作为接收方,将该新变量作为正文调用邮件函数。当循环循环时,它将创建一个带有消息正文的新 var 和指向该其他订阅者的新链接。