【问题标题】:How to generate unsubscribe link in email?如何在电子邮件中生成退订链接?
【发布时间】:2016-05-30 11:56:49
【问题描述】:

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

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

【问题讨论】:

    标签: php html email unsubscribe


    【解决方案1】:

    获取用户列表后在 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
    }
    

    【讨论】:

    • 我试过了,但不幸的是我得到了和以前一样的结果。
    • 你的邮件功能在foreach和$dtl =$post. "\n\n";没有点
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 2019-02-01
      • 2011-03-24
      • 1970-01-01
      • 2018-06-20
      • 2012-03-24
      • 1970-01-01
      相关资源
      最近更新 更多