【问题标题】:Syntax error, unexpected T_SL语法错误,意外的 T_SL
【发布时间】:2011-07-21 22:43:57
【问题描述】:

我对 php 还很陌生,我正在使用一个脚本来创建一个名为“mime_mailer”的函数,它基本上允许我使用 PHP 发送能够用 CSS 设计的电子邮件,而不仅仅是纯文本.

然而,在我的注册脚本中,我尝试编写一些发送 CSS 电子邮件的代码,但我收到一条错误消息,指出存在语法错误。有人可以帮我填写一下吗?

            $subject = "Your Red-line Account";
    $css     = "body{ color: #090127; background-color: #f0f0f0; }"; 
    $to     =   $usercheck;

    //Message
    $message =<<<END 
                <html>
                    <head>
                        <title>
                            Red-line
                        </title>
                    </head>
                    <body>
                        <p>
                            Hi $first_name, 
                        </p> 

                        <p>
                            Your Red-line account is almost complete. To finish, go to <a href='www.thered-line.com'>The Red-line</a> and enter your eight digit confirmation code.
                        </p> 

                        <p>
                            Your confirmation code is: <b>$code</b>
                        </p> 

                        <p>
                            Sincerely,
                        </p> <br />

                        <p>
                            The Red-line Operator
                        </p> 
                    </body>
                </html>
            END;

                    //  To send HTML mail, the Content-type header must be set
        $headers    =   'MIME-Version: 1.0' . "\r\n";
        $headers    .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

                    //  Additional headers
        $headers    .=  "From: The Red-line <messages@theredline.com>\r\n";
        $headers    .=  "To: $first_name $last_name <$usercheck>\r\n";

                    //  Mail it
        require_once("function_mime_mailer.php");


        mime_mailer($to, $subject, $message, $headers, NULL, $css); 
}

这是“function_mime_mailer.php”文件的代码。

  if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404(); // stop http access           to         this file

 function mime_mailer($to, $subject, $message, $headers = NULL, $attachments = NULL, $css = NULL)
 {
       if(!preg_match('/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-        z]{2,6})$/', $to)) return FALSE;
if(preg_match('/<(html|head|body|div|a|h|p|table|br|img|b|hr|ol|ul|span|pre|i|form)[^>]*[^>]*>/i', $message)) $html = TRUE;

 if(stristr($message, '<body')) $message = stristr($message, '<body');
     $message = delete_local_links($message);
 if(empty($headers)){
     $headers = "MIME-Version: 1.0\n";
 }else{
     $headers.= "\nMIME-Version: 1.0\n";
 }
 if(empty($html)){
     $result = plain_text($message);
 }elseif(isset($html) and $html == TRUE){
     if(!isset($css)) $css = NULL;
     if(preg_match('/<img[^>]+>/i', $message)){
       $result = multipart_related($message, $css);
   }else{
       $result = multipart_alternative($message, $css);
   }
 }
 $result['message'] = delete_non_cid_images($result['message']);
 if(!empty($attachments)){
   $parts = attachments($attachments);
   array_unshift($parts, implode('', $result));
   $result = multipart_mixed($parts);
 }
$headers = $headers.$result['headers'];
//print '<pre>'.htmlspecialchars($headers.$result['message']).'</pre>';exit;
if(mail($to, $subject, $result['message'], $headers)) return TRUE;
return FALSE;
}
?> 

【问题讨论】:

  • 发布指示行号的错误消息并在发布的代码中定义该行号
  • 错误在注册脚本中。它在第 6 行。定义 $message 变量的行。

标签: mime php


【解决方案1】:

刚刚遇到同样的问题。

原来是内容和我开场的HERDEOC在同一行

错误的例子

echo <<<HEREDOC code started on this line
HEREDOC;

正确的例子

echo <<<HEREDOC
code should have started on this line
HEREDOC;

希望这对其他人有帮助!

【讨论】:

  • 值得指出的是,即使是空格字符(可能隐藏在您的编辑器中)也可能导致此问题。 那是在你找到它之前是个脑筋急转弯。
【解决方案2】:

看看List of Parser tokens

T_SL 引用 &lt;&lt;

在使用END; 之前,不应使用制表符或空格。看看this huge warning

【讨论】:

  • 我将
  • @Lance:你应该把&lt;&lt;END改回&lt;&lt;&lt;END。您应该只删除与END; 在同一行的制表符和空格。
【解决方案3】:

附注,但可能会对某人有所帮助:糟糕的 git merge 可能会导致此问题。考虑:

function foo
    <<<<<<< HEAD
    $bar = 1;
    <<<<<<<  e0f2213bc34d43ef
    $bar = 2;

PHP 解析器会产生同样的错误。

来源:刚刚被这个;)

【讨论】:

  • 谢谢,我也是这样,怎么解决呢?我不知道哪个文件包含错误
  • 我很抱歉 Shireef,我没有回到这个......在 2020 年初的职业跳跃和疯狂的忙碌时间让我在很大程度上远离了 Stack。如果您有类似 UNIX 的系统,我建议使用 grep 或 find piped to grep ....
【解决方案4】:

它有完全相同的问题,但我的问题是因为我在第一行的 heredoc 末尾有空格:

$var = <<< HTML(whitespaces here caused the error)
stuff in here

HTML;

来源:http://realtechtalk.com/_syntax_error_unexpected_T_SL_in_PHP_Solution-2109-articles

【讨论】:

    【解决方案5】:

    你用的是什么版本的php? nowdoc 语法仅在 PHP 5.3.0 之后才有效。 参见手册: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

    【讨论】:

    • 我最近(2017 年!)遇到了这个错误,这就是原因。我的网络托管公司仍在使用 PHP 5.2.9(我认为自 2014 年以来不受支持),它会为 nowdoc 引发此错误。记得检查您的虚拟主机的 PHP 版本。
    【解决方案6】:

    function_mime_mailer.php 中有一个错误:

    if(empty($headers)){
       $headers = "MIME-Version: 1.0\n";
    }else{
       $headers.= "\nMIME-Version: 1.0\n";
    }
    

    应该是

    if(empty($headers)){
       $headers = "MIME-Version: 1.0\r\n";
    }else{
       $headers.= "\r\nMIME-Version: 1.0\r\n";
    }
    

    此外,如果您包含 MIME-Version 标头,则该函数将再次包含它 - 实际上有两个。

    【讨论】:

      【解决方案7】:

      问题与代码中不必要的空格/制表符有关...

      在使用 get merge feature/branchname 后遇到了这个问题。我们使用 Prettier 作为格式化程序,因此在接受这两项更改后 - 保存文件,解决了问题。

      【讨论】:

        猜你喜欢
        • 2012-06-27
        • 1970-01-01
        • 1970-01-01
        • 2016-12-28
        • 2018-08-03
        • 1970-01-01
        • 2011-10-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多