Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Swiftmailer接收器';正文中数据库中的名称_Php_Database_Email_Foreach_Swiftmailer - Fatal编程技术网

PHP Swiftmailer接收器';正文中数据库中的名称

PHP Swiftmailer接收器';正文中数据库中的名称,php,database,email,foreach,swiftmailer,Php,Database,Email,Foreach,Swiftmailer,你好,我正在使用Swiftmailer发送一份时事通讯,我想在邮件正文的开头使用数据库中的他们的名字我已经成功地在邮件正文和收件人中获得了一个名字,但是当我想在正文中使用名字时,它只从数据库中获取第一个条目,而不使用第二个或第三个条目 foreach ($result as $row) { if ($_POST['test_or_live'] == 'live') { $email[] = array($row['mail'] => $r

你好,我正在使用Swiftmailer发送一份时事通讯,我想在邮件正文的开头使用数据库中的他们的名字我已经成功地在邮件正文和收件人中获得了一个名字,但是当我想在正文中使用名字时,它只从数据库中获取第一个条目,而不使用第二个或第三个条目

    foreach ($result as $row) {         
    if ($_POST['test_or_live'] == 'live') { 
        $email[] = array($row['mail'] => $row['firstname']); //for the mail address and firstname in the receiver. works
        $firstname[] = $row['firstname']; //for the firstname in the body. not working
    }
这是我用来送出的名字,并把名字放在身体里

foreach ($email as $mail => $name) {

    foreach ($firstname as $value) {
        $content = str_replace('firstname',$value,$content);
    }

    if (is_int($mail)) {
        $message->setTo($name);
    } 

    else {
        $message->setTo(array($mail => $name));
    }

    $message->setBody($content,'text/html');
    $numSent += $mailer->send($message, $failedRecipients);
}
那么,如何让它在所有名称上循环,并将正确的名称发送到循环中的正确邮件地址

foreach ($firstname as $value) {
    $content = str_replace('firstname',$value,$content);
}
您正在将第一次迭代中的模板占位符“firstname”替换为名称

因为名字在外部循环中作为
$name

foreach ($email as $mail => $name)
您不需要
$firstname[]
数组:

foreach ($email as $mail => $name) {
    $content = str_replace('firstname',$name,$content);
    ...

当我这样做时,它会输出“Array”,您能提供
$email
数组的示例吗?$email[]=Array($row['mail']=>$row['firstname']);是在原来的问题中,但你看,还是说数据库里有什么?数据库中的firstname包含“hello”和“bye”对不起,我的错,那么它看起来像
Array([0]=>Array([mail1]=>firstname1),[1]=>Array([mail2]=>firstname2).
?不,它只是字面上说的是“Array”