取消订阅时事通讯

发布于 2025-01-03 17:22:05 字数 1602 浏览 1 评论 0原文

我想添加一个 linf 来取消签署我的时事通讯。我无法成功在时事通讯中提供我的变量“电子邮件”。 我同时将简讯发送到某个电子邮件地址 $template 允许新闻通讯从数据库获取数据,我的新闻通讯在 html 中,变量如下:{{variable}} 我尝试将我的代码模板放在我的 while 中,但电子邮件仍然是第一个 emai。 你有主意吗?

这是代码:

if($_POST['recipients']) {

    $template = file_get_contents('template/emails/newsletter.html');

    $addresses = explode(';', $_POST['recipients']);
    $subject = stripslashes($_POST['subject']);
    foreach($addresses as $address) {

    $newsletter_unsign =  ' <a href="http://dev.cater2.me/unsign-newsletter.php?id='.$address.'">Unsubscribe from the newsletter</a>.';

    $template = str_replace(array('{{newsletter_top_bar}}','{{newsletter_top}}','{{newsletter_title1}}','{{newsletter_body1}}','{{newsletter_title2}}','{{newsletter_body2}}','{{newsletter_title3}}','{{newsletter_body3}}','{{newsletter_side_title}}','{{newsletter_side}}','{{newsletter_bottom}}','{{newsletter_footer}}','{{newsletter_unsign}}'),
                  array($_POST['newsletter_top_bar'],$_POST['newsletter_top'],$_POST['newsletter_title1'],$_POST['newsletter_body1'],$_POST['newsletter_title2'],$_POST['newsletter_body2'],$_POST['newsletter_title3'],$_POST['newsletter_body3'],$_POST['newsletter_side_title'],$_POST['newsletter_side'],$_POST['newsletter_bottom'],$_POST['newsletter_footer'], $newsletter_unsign),
                  $template);


        $address = trim($address);

        sendMail($address, $subject, str_replace('{{c2me_tracking_link}}', 'http://cater2.me/?ref='.urlencode(c2me_encrypt($address)), $template), true);
    }

    notif('E-mail(s) sent');
}

}

I want to add a linf to unsign to my newsletter. I can't succeed to give my variable "email" in the newsletter.
I send the newsletter to some email in the same time
$template allow to the newsletter to get data from database,and my newsletter in in html with variables like this: {{variable}}
I tried to put my code template in my while, but the email is still the first emai.
Do you have an idea?

here's the code:

if($_POST['recipients']) {

    $template = file_get_contents('template/emails/newsletter.html');

    $addresses = explode(';', $_POST['recipients']);
    $subject = stripslashes($_POST['subject']);
    foreach($addresses as $address) {

    $newsletter_unsign =  ' <a href="http://dev.cater2.me/unsign-newsletter.php?id='.$address.'">Unsubscribe from the newsletter</a>.';

    $template = str_replace(array('{{newsletter_top_bar}}','{{newsletter_top}}','{{newsletter_title1}}','{{newsletter_body1}}','{{newsletter_title2}}','{{newsletter_body2}}','{{newsletter_title3}}','{{newsletter_body3}}','{{newsletter_side_title}}','{{newsletter_side}}','{{newsletter_bottom}}','{{newsletter_footer}}','{{newsletter_unsign}}'),
                  array($_POST['newsletter_top_bar'],$_POST['newsletter_top'],$_POST['newsletter_title1'],$_POST['newsletter_body1'],$_POST['newsletter_title2'],$_POST['newsletter_body2'],$_POST['newsletter_title3'],$_POST['newsletter_body3'],$_POST['newsletter_side_title'],$_POST['newsletter_side'],$_POST['newsletter_bottom'],$_POST['newsletter_footer'], $newsletter_unsign),
                  $template);


        $address = trim($address);

        sendMail($address, $subject, str_replace('{{c2me_tracking_link}}', 'http://cater2.me/?ref='.urlencode(c2me_encrypt($address)), $template), true);
    }

    notif('E-mail(s) sent');
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2025-01-10 17:22:05
if($_POST['recipients']) {

    $original_template = file_get_contents('template/emails/newsletter.html');

    $addresses = explode(';', $_POST['recipients']);
    $subject = stripslashes($_POST['subject']);

    foreach($addresses as $address) {

      // You should have made sure before/outside the foreach loop, that the e-mails are well-formatted and valid
      $address = trim($address);

      $this_template = $original_template;

      $newsletter_unsign =  ' <a href="http://dev.cater2.me/unsign-newsletter.php?id='.$address.'">Unsubscribe from the newsletter</a>.';

      $this_template = str_replace(array('{{newsletter_top_bar}}','{{newsletter_top}}','{{newsletter_title1}}','{{newsletter_body1}}','{{newsletter_title2}}','{{newsletter_body2}}','{{newsletter_title3}}','{{newsletter_body3}}','{{newsletter_side_title}}','{{newsletter_side}}','{{newsletter_bottom}}','{{newsletter_footer}}','{{newsletter_unsign}}'),
                  array($_POST['newsletter_top_bar'],$_POST['newsletter_top'],$_POST['newsletter_title1'],$_POST['newsletter_body1'],$_POST['newsletter_title2'],$_POST['newsletter_body2'],$_POST['newsletter_title3'],$_POST['newsletter_body3'],$_POST['newsletter_side_title'],$_POST['newsletter_side'],$_POST['newsletter_bottom'],$_POST['newsletter_footer'], $newsletter_unsign),
                  $this_template);

      sendMail($address, $subject, str_replace('{{c2me_tracking_link}}', 'http://cater2.me/?ref='.urlencode(c2me_encrypt($address)), $this_template), true);
    }
    notif('E-mail(s) sent');

}

if($_POST['recipients']) {

    $original_template = file_get_contents('template/emails/newsletter.html');

    $addresses = explode(';', $_POST['recipients']);
    $subject = stripslashes($_POST['subject']);

    foreach($addresses as $address) {

      // You should have made sure before/outside the foreach loop, that the e-mails are well-formatted and valid
      $address = trim($address);

      $this_template = $original_template;

      $newsletter_unsign =  ' <a href="http://dev.cater2.me/unsign-newsletter.php?id='.$address.'">Unsubscribe from the newsletter</a>.';

      $this_template = str_replace(array('{{newsletter_top_bar}}','{{newsletter_top}}','{{newsletter_title1}}','{{newsletter_body1}}','{{newsletter_title2}}','{{newsletter_body2}}','{{newsletter_title3}}','{{newsletter_body3}}','{{newsletter_side_title}}','{{newsletter_side}}','{{newsletter_bottom}}','{{newsletter_footer}}','{{newsletter_unsign}}'),
                  array($_POST['newsletter_top_bar'],$_POST['newsletter_top'],$_POST['newsletter_title1'],$_POST['newsletter_body1'],$_POST['newsletter_title2'],$_POST['newsletter_body2'],$_POST['newsletter_title3'],$_POST['newsletter_body3'],$_POST['newsletter_side_title'],$_POST['newsletter_side'],$_POST['newsletter_bottom'],$_POST['newsletter_footer'], $newsletter_unsign),
                  $this_template);

      sendMail($address, $subject, str_replace('{{c2me_tracking_link}}', 'http://cater2.me/?ref='.urlencode(c2me_encrypt($address)), $this_template), true);
    }
    notif('E-mail(s) sent');

}

帅的被狗咬 2025-01-10 17:22:05

您仅从文件中获取 $template 一次,但随后尝试多次替换它。第一次替换后,它会被固定为第一个收件人的数据。

尝试将替换的字符串分配给新变量,例如 $template_after_replace

if($_POST['recipients']) {

  $template = file_get_contents('template/emails/newsletter.html');

  $addresses = explode(';', $_POST['recipients']);
  $subject = stripslashes($_POST['subject']);
  foreach($addresses as $address) {

    $newsletter_unsign =  ' <a href="http://dev.cater2.me/unsign-newsletter.php?id='.$address.'">Unsubscribe from the newsletter</a>.';

    $template_after_replace = str_replace(array('{{newsletter_top_bar}}','{{newsletter_top}}','{{newsletter_title1}}','{{newsletter_body1}}','{{newsletter_title2}}','{{newsletter_body2}}','{{newsletter_title3}}','{{newsletter_body3}}','{{newsletter_side_title}}','{{newsletter_side}}','{{newsletter_bottom}}','{{newsletter_footer}}','{{newsletter_unsign}}'),
              array($_POST['newsletter_top_bar'],$_POST['newsletter_top'],$_POST['newsletter_title1'],$_POST['newsletter_body1'],$_POST['newsletter_title2'],$_POST['newsletter_body2'],$_POST['newsletter_title3'],$_POST['newsletter_body3'],$_POST['newsletter_side_title'],$_POST['newsletter_side'],$_POST['newsletter_bottom'],$_POST['newsletter_footer'], $newsletter_unsign),
              $template);


    $address = trim($address);

    sendMail($address, $subject, str_replace('{{c2me_tracking_link}}', 'http://cater2.me/?ref='.urlencode(c2me_encrypt($address)), $template_after_replace), true);
  }

  notif('E-mail(s) sent');
}

作为一项改进,您只需运行替换一次(在 foreach 循环之前替换那些不会出现的内容) t 更改(例如 $_POST['newsletter_top']),然后在 foreach 循环中单独替换 $newsletter_unsign,但我将其保留为OP 的练习。

You only fetch $template from the file once, but then you try to replace it multiple times. After the first replace, it is fixed with the first recipient's data.

Try assigning the replaced string to a new variable, e.g. $template_after_replace:

if($_POST['recipients']) {

  $template = file_get_contents('template/emails/newsletter.html');

  $addresses = explode(';', $_POST['recipients']);
  $subject = stripslashes($_POST['subject']);
  foreach($addresses as $address) {

    $newsletter_unsign =  ' <a href="http://dev.cater2.me/unsign-newsletter.php?id='.$address.'">Unsubscribe from the newsletter</a>.';

    $template_after_replace = str_replace(array('{{newsletter_top_bar}}','{{newsletter_top}}','{{newsletter_title1}}','{{newsletter_body1}}','{{newsletter_title2}}','{{newsletter_body2}}','{{newsletter_title3}}','{{newsletter_body3}}','{{newsletter_side_title}}','{{newsletter_side}}','{{newsletter_bottom}}','{{newsletter_footer}}','{{newsletter_unsign}}'),
              array($_POST['newsletter_top_bar'],$_POST['newsletter_top'],$_POST['newsletter_title1'],$_POST['newsletter_body1'],$_POST['newsletter_title2'],$_POST['newsletter_body2'],$_POST['newsletter_title3'],$_POST['newsletter_body3'],$_POST['newsletter_side_title'],$_POST['newsletter_side'],$_POST['newsletter_bottom'],$_POST['newsletter_footer'], $newsletter_unsign),
              $template);


    $address = trim($address);

    sendMail($address, $subject, str_replace('{{c2me_tracking_link}}', 'http://cater2.me/?ref='.urlencode(c2me_encrypt($address)), $template_after_replace), true);
  }

  notif('E-mail(s) sent');
}

As an improvement, you can just run the replacement once (before the foreach loop to replace things that won't change (e.g $_POST['newsletter_top']), then individually replace $newsletter_unsign within the foreach loop, but I will leave that as an exercise for the OP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文