【问题标题】:PHP Mail using HTML with PHP if statement embeddedPHP Mail 使用 HTML 和 PHP if 语句嵌入
【发布时间】:2014-08-09 00:24:12
【问题描述】:

我目前让 PHP 发送一封 HTML 电子邮件,其中列出了表单中的每个项目,即使数量为 0。现在我想将其更改为仅列出正在订购的项目 (Qty>0)。在互联网上寻找并找到一种在 HTML 电子邮件中嵌入 PHP if 语句的方法后,这就是我想出的,但它仍然无法正常工作......

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];

$AV000100 = $_POST['AV000100'];
$AV000101 = $_POST['AV000101'];
$AV000102 = $_POST['AV000102'];

$comments = $_POST['comments'];
?>

<?php
$to = "me@company.com";
$subject = "Marketing Material Order from $name";
$message = "
<html>
<head></head>
<body>
    <p>
        Name: $name<br>
        Email: $email<br>
        Date Needed: $date
    </p>

    <?php if ($AV000100 != 0) {
        $AV000100 - Brochures;
        }
    ?>

    <?php if ($AV000101 != 0) {
        $AV000101 - Folders;
        }
    ?>

    <?php if ($AV000102 != 0) {
        $AV000102 - Pens;
        }
    ?>

<p>Comments: $comments</p>

</body>
</html>
";

$headers = "From: $email" . "\r\n" .
$headers = "Reply-To: $email" . "\r\n" .
$headers = 'MIME-Version: 1.0' . "\r\n" .
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail($to, $subject, $message, $headers);
?>

如何更改嵌入的 PHP if 语句以将其显示为电子邮件正文...

姓名:约翰·多伊 电子邮件:jon@mail.com 所需日期:今天 2 - 小册子 2 - 钢笔 点评:谢谢

而不是...

姓名:约翰·多伊 电子邮件:jon@mail.com 所需日期:今天 2 - 小册子 0 - 文件夹 2 - 钢笔 点评:谢谢

仅供参考...我有上面写的 PHP if 语句的方式,没有任何项目出现。任何帮助将不胜感激!

【问题讨论】:

    标签: php html email


    【解决方案1】:

    不太确定你想要什么 - 但如果你想使用评估 PHP 代码的结果作为消息体,那么你需要使用输出缓冲(或大量的内联 ifs - 输出缓冲更干净)。

    例如,如果您将生成消息正文的所有代码放在文件 body.php 中,那么您可以使用:

    ob_start();
    include('body.php');
    $message = ob_get_Clean();
    

    您不需要将正文代码放在单独的文件中 - 只需包含它而不是 ob_start()ob_get_clean() 之间的包含语句

    【讨论】:

      【解决方案2】:

      这就是你想用你拥有的东西做的事情。

      $message = "
      <html>
      <head></head>
      <body>
          <p>
              Name: $name<br>
              Email: $email<br>
              Date Needed: $date
          </p>
      
          " . ($AV000100 != 0 ? $AV000100 . " - Brochures" : "") . "
      
          " . ($AV000101 != 0 ? $AV000101 . " - Folders" : "") . "
      
          " . ($AV000102 != 0 ? $AV000102 . " - Pens" : "") . "
      
          <p>Comments: $comments</p>
      </body>
      </html>";
      

      【讨论】:

        【解决方案3】:

        如果您想使用连接赋值运算符 (.=),您可以逐行附加消息,并将 if 语句放在您需要的地方。像这样:

        $message = "<html>";
        $message .= "<head></head>";
        $message .= "<body>";
        $message .= "<p>";
        $message .= "Name: $name<br>";
        $message .= "Email: $email<br>";
        $message .= "Date Needed: $date";
        $message .= "</p>";
        if ($AV000100 != 0) {
        $message .= "$AV000100 - Brochures";
        }
        if ($AV000101 != 0) {
        $message .= "$AV000101 - Folders";
        }
        if ($AV000102 != 0) {
        $message .= "$AV000102 - Pens";
        }
        
        $message .= "<p>Comments: $comments</p>";
        $message .= "</body>";
        $message .= "</html>";
        

        您可以像往常一样发送$message

        这是完整的代码:

        <?php
        $name = $_POST['name'];
        $email = $_POST['email'];
        $date = $_POST['date'];
        
        $AV000100 = $_POST['AV000100'];
        $AV000101 = $_POST['AV000101'];
        $AV000102 = $_POST['AV000102'];
        $comments = $_POST['comments'];
        
        $to = "me@company.com";
        $subject = "Marketing Material Order from $name";
            $message = "<html>";
            $message .= "<head></head>";
            $message .= "<body>";
            $message .= "<p>";
            $message .= "Name: $name<br>";
            $message .= "Email: $email<br>";
            $message .= "Date Needed: $date";
            $message .= "</p>";
            if ($AV000100 != 0) {
            $message .= "$AV000100 - Brochures";
            }
            if ($AV000101 != 0) {
            $message .= "$AV000101 - Folders";
            }
            if ($AV000102 != 0) {
            $message .= "$AV000102 - Pens";
            }
        
            $message .= "<p>Comments: $comments</p>";
            $message .= "</body>";
            $message .= "</html>";
        
        $headers = "From: $email" . "\r\n" .
        $headers = "Reply-To: $email" . "\r\n" .
        $headers = 'MIME-Version: 1.0' . "\r\n" .
        $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        
        mail($to, $subject, $message, $headers);
        ?>
        

        【讨论】:

          【解决方案4】:

          这样试试

          $message = "
          <html>
          <head></head>
          <body>
              <p>
                  Name: $name<br>
                  Email:$email<br>
                  Date Needed:$date
              </p>
           ...
           ...";
          

          每次您断开字符串,然后使用. 运算符将字符串与php 结果连接起来。

          【讨论】:

          • 你不能在这样的字符串中echo
          • Soory @Rolice ..感谢您指出..我已经编辑了我的答案..您能否删除该反对票...
          • 是的,@Lal。现在是准确的。
          • 如果你使用双引号,那么你不需要从引用中出来来包含简单的变量。 $message="something".$else;$message="something$else"; 相同
          猜你喜欢
          • 2017-02-15
          • 2017-05-17
          • 2010-10-17
          • 2015-07-07
          • 2016-06-11
          • 1970-01-01
          • 1970-01-01
          • 2013-02-27
          相关资源
          最近更新 更多