PHP Mailer - 消息体中的foreach

时间:2014-01-17 15:59:45

标签: php foreach phpmailer

我想在电子邮件正文中使用foreach包含结果。当我添加foreach语句时,PHP Mailer似乎只是切断了消息。关于如何在邮件正文中包含多个结果的任何想法?

$mail->Body    = '*** Automated email - do not reply ***<br><br>Hello '.$Name.', <br><br>We were able to connect you with the following services. <br><br> ';
foreach($findResult['data'] as $key=>$findData){ 
 if($findData['CompanyEmail'][0] != NULL || $findData['CompanyPhone'][0] != NULL) { 
     echo $findData['CompanyName'][0]; 
     echo $findData['CompanyContact'][0]; 
     echo $findData['CompanyPhone'][0];
     echo $findData['ContactEmail'][0];
     }?><br><?php }
$body .='<br><br>Thank you for using our directory.<br><br><br>Regards,<br><br>Sender';

P.S。如果我拿出';'在foreach之前,我收到了一个错误。

1 个答案:

答案 0 :(得分:8)

您需要使用$mail->Body .=来继续使用echo,而不是使用echo。像这样:

$mail->Body    = '*** Automated email - do not reply ***<br><br>Hello '.$Name.', <br><br>We were able to connect you with the following services. <br><br> ';
foreach($findResult['data'] as $key=>$findData){ 
    if($findData['CompanyEmail'][0] != NULL || $findData['CompanyPhone'][0] != NULL) { 
        $mail->Body .= $findData['CompanyName'][0]; 
        $mail->Body .= $findData['CompanyContact'][0]; 
        $mail->Body .= $findData['CompanyPhone'][0];
        $mail->Body .= $findData['ContactEmail'][0];
    }
}
$body .='<br><br>Thank you for using our directory.<br><br><br>Regards,<br><br>Sender';

如果我怀疑$body也应该成为电子邮件的一部分,那么也可以将其更改为$mail->Body .=