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

时间:2016-02-18 10:53:06

标签: php html email unsubscribe

我有一个时事通讯系统,所以当我想向所有订阅者发送消息时,我通过名为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

我不希望配方回复取消订阅所有订阅者的链接。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

获取用户列表后,将您的电子邮件代码绑定到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
}

答案 1 :(得分:0)

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