在PHPMailer中的msgHTML中运行foreach循环

时间:2014-02-20 23:41:08

标签: php

我想在PHP Mailer上的msgHTML中运行一个foreach循环,以使表td从数组动态化。以下是我的最新代码。我正在使用Dreamweaver并且它显示此代码有错误,我不知道如何解决这个问题。

<?php
$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title;
$mail->MsgHTML(
"
Some html and text goes here....
Below is the main part of this topic.

<u><strong>Referees & Contact</strong></u><br/>
<table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'>
    <tr style='background: #003e82; color: #FFF; font-weight: bold;'>
        <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
        <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
        <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
        <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
    </tr>
"
?>
    <?php
        foreach($variable as $var){
    ?>
        <tr>
            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
        </tr>
    <?php
        }
    ?>
    <?php "
</table>
");
?>

2 个答案:

答案 0 :(得分:1)

这段代码怎么样?

$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title;

$msg = "Some html and text goes here....
   Below is the main part of this topic.

   <u><strong>Referees & Contact</strong></u><br/>
   <table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'>
    <tr style='background: #003e82; color: #FFF; font-weight: bold;'>
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
      <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
    </tr>";

foreach($variable as $var){

   $msg .= "<tr>
            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
        </tr>";

} // foreach

$msg .= "</table>";

$mail->MsgHTML( $msg );

答案 1 :(得分:1)

试试这个

$mail->Subject = "Vacancy No: " . $vacancy_no . " - " . $vacancy_title;


        $message =
                "
                Some html and text goes here....
                Below is the main part of this topic.

                <u><strong>Referees & Contact</strong></u><br/>
                <table width='100%' border='0' cellpadding='0' cellspacing='0' style='border: 1px solid #E2E2E2;'>
                    <tr style='background: #003e82; color: #FFF; font-weight: bold;'>
                        <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
                        <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
                        <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
                        <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
                    </tr>
                ";

        foreach ($variable as $var) {

            $message .= "<tr>
                            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Name</td>
                            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Position & Organization</td>
                            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Phone Contact</td>
                            <td style='border: 1px solid #E2E2E2; height:30px; padding-left:5px; padding-right: 5px;'>Email</td>
                        </tr>";
        }


        $message .= "
                </table>
                ";

        $mail->MsgHTML($message);