php - 获取每封电子邮件的用户名

标签 php html mysql

首先是我的代码:

<?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/

相关文章:

javascript - 如何使用 JQuery 为数组的每个值生成 HTML 元素?

php - 从数据库创建 CSV 文件时启用 PHP 错误

PHP/MySQL : Order artist by ASC, 然后按 ASC 按艺术家排序歌曲?

javascript - 读取输入标签上输入的值

html - HTML声音拒绝循环播放

mysql - 如何使用 SQL SELECT 在单个报告中获取选择器和选择器的名称?

php - 为我网站上的任意元素创建 "Like"按钮,并读回状态 - 可能吗?

html - Bootstrap 导航栏图标栏不起作用

php - 每个用户有多个 SQL 表?

php - 获取行数限制值sql php