我想在我的简报中添加一个linf来取消签名。我无法在通讯中提供变量“email”。 我同时将电子报发送给某个电子邮件 $ template允许时事通讯从数据库获取数据,我的时事通讯在html中使用如下变量:{{variable}} 我试着把我的代码模板放进去,但电子邮件仍然是第一封电子邮件。 你有什么想法吗?
这是代码:
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');
}
}
答案 0 :(得分:0)
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');
}
答案 1 :(得分:0)
您只从文件中提取$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
循环之前替换不会更改的内容(例如$_POST['newsletter_top']
),然后单独替换$newsletter_unsign
内的foreach
{{1}}循环,但我将把它作为OP的练习。