【问题标题】:PHP - Sending Email HTML/Plain Text - Syntax Error?PHP - 发送电子邮件 HTML/纯文本 - 语法错误?
【发布时间】:2013-11-30 21:48:17
【问题描述】:

我正在尝试实现一个简单的 sendEmails 函数,如果可能,它会发送 HTML,如果不是,它会发送纯文本。但是,由于某种原因,我在文件中遇到语法错误,即使我通过在线验证器(例如 phpcodechecker)传递它,它也不会出现任何问题。

确切的错误:

解析错误:语法错误,第 83 行 C:\xampp\htdocs..\func.php 中的文件意外结束

注意:我只实现了 sendEmails 函数,它破坏了它。

我的代码

<?php

    //Input: Email | Output: Boolean for Domain Existence (such as @yahoo.com) [TESTED]
    function domainExists($email, $record = 'MX'){
        list($user, $domain) = explode('@', $email);
        return checkdnsrr($domain, $record);
    }

    function sendEmails($to, $type, $link){
        switch($type){
            case "register":
                if(strlen($link)>0) $authenticationLink = $link;
                else throwError("Email Failed",1);
                //Send Email
                $subject = "Thank You For Registering";
                $boundaryHash = md5(date('r',time()));
                $headers = "From: admin@....com";
                $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$boundaryHash."\"";
                ob_start();
                ?>
                    --PHP-alt-<?php echo $boundaryHash; ?> 
                    Content-Type: text/plain; charset="iso-8859-1"
                    Content-Transfer-Encoding: 7bit

                    Thank you for registering with ...! 
                    We hope you enjoy our service! To get started, please authenticate your account by copying and pasting the following link:
                    <?php echo $authenticationLink; ?>

                    --PHP-alt-<?php echo $boundaryHash; ?> 
                    Content-Type: text/html; charset="iso-8859-1"
                    Content-Transfer-Encoding: 7bit

                    <h2>Thank You For Registering!</h2>
                    <p>Thank you for registering with ...!</p>

                    <p>We hope you enjoy our service! To get started, please authenticate your account with the following link:</p>
                    <a href="<?php echo $authenticationLink; ?>">Click to Authenticate</a>

                    --PHP-alt-<?php echo $boundaryHash; ?>--
                <?
                $message = ob_get_clean();
                $mail_sent = mail( $to, $subject, $message, $headers );
                echo $mail_sent ? "Mail sent" : "Mail failed";
                break;
            default: break;
        }
        return;
    }

    //isEscaped(String, Con):boolean, [TESTED]
    function isEscaped($str, $con){
        return (mysqli_real_escape_string($con, $str) === $str);
    }


    //Type: | 0: Unknown Error | 1:  | 2: DB Error | 3: Security Issue - Unescaped |
    //String: | 'Fatal' | 'NonFatal' | 'Trivial' | Otherwise String will be output
    //[TESTED]
    function throwError($str, $type){
        //Making sure parameters are valid
        $t = (int)$type; 
        $s = (string)$str;  

        //Shorthand to auto output general errors
        switch ($s){ 
            case 'Fatal': 
                $s = "FATAL ERROR: Please contact the administrator immediately!"; 
                break;
            case 'NonFatal':
                $s = "ERROR: Please contact the administrator!";
                break;
            case 'Trivial':
                $s = "Unexpected Error: Please go back and try again!";
                break;
        }

        echo "<p class='errorspan'>".$s.": ".$t."</p>";
        exit($t);
    }?>

【问题讨论】:

  • 确保打开的标签是“ON”,你有&lt;? $message = ob_get_clean();,这可能是其中之一。
  • 加上unexpected end of file 意味着您在某处缺少右大括号或左大括号太多,这可能来自显然包含的文件。您发布的代码中只有 79 行,共 83 行。
  • 谢谢!我没有看到'
  • 那么问题是,是吗?
  • 我可以输入一个答案,这样你就可以有一个干净的记录而不是删除你的问题。

标签: php html email text syntax-error


【解决方案1】:

确保打开的标签是“ON”,你有&lt;? $message = ob_get_clean();,这可能是其中之一。

改成:

<?php $message = ob_get_clean();

另外,unexpected end of file 表示您在某处缺少右大括号或左大括号太多,这可能来自明显包含的文件。

您发布的代码中只有 79 行,总共 83 行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 2012-04-29
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2021-04-12
    • 2017-04-23
    相关资源
    最近更新 更多