首先是我的代码:
<?php
if(isset($betreff) && isset($text)){
$query= "SELECT email, anrede, vorname, nachname FROM newsletter";
$result= mysql_query ($query);
while ($row = mysql_fetch_array($result)) {
$anrede= $row['anrede'];
$vorname= $row['vorname'];
$nachname= $row['nachname'];
$email= $row['email'];
$text = str_replace('[A]', $anrede, $text);
$text = str_replace('[V]', $vorname, $text);
$text = str_replace('[N]', $nachname, $text);
$text = str_replace('[E]', $email, $text);
$body=$text;
strip_tags($text, '<br><b><img><a>');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Commeatus-IO <test@test.de>';
mail($email, $betreff, $body, $headers);
echo 'Email wurde gesendet an: ' . $email. '<br>';
}
?>
现在,如果我在文本中插入 [V]
(对于姓氏)或 [E]
用于电子邮件。但如果我这样做,它会在每封电子邮件中显示相同的姓氏和名字。为什么?
谢谢
最佳答案
这是因为您正在覆盖包含 [A][N][V][E]
占位符的文本,并且后续迭代无法再找到它们。
首先从 $text 提供你的 $body
变量,这样你就不会覆盖你的模板。
您还缺少循环末尾的右大括号。
您的代码应如下所示:
<?php
if(isset($betreff) && isset($text)){
$query= "SELECT email, anrede, vorname, nachname FROM newsletter";
$result= mysql_query ($query);
while ($row = mysql_fetch_array($result)) {
$anrede= $row['anrede'];
$vorname= $row['vorname'];
$nachname= $row['nachname'];
$email= $row['email'];
// feed $body with your template ($text)
$body = $text;
// use $body as the working copy (replace the placeholders in $body)
$body = str_replace('[A]', $anrede, $body);
$body = str_replace('[V]', $vorname, $body);
$body = str_replace('[N]', $nachname, $body);
$body = str_replace('[E]', $email, $body);
// I assume you wanted to strip tags in the e-mail text, not in the template
strip_tags($body, '<br><b><img><a>');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Commeatus-IO <test@test.de>';
mail($email, $betreff, $body, $headers);
echo 'Email wurde gesendet an: ' . $email. '<br>';
}
}
?>
关于php - 获取每封电子邮件的用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33254651/