-3

谁能帮助我理解我的代码中发生了什么?它是编程错误还是由于php引擎出现故障?以下是我正在制作的一个非常容易发送电子邮件的类的代码。
我想要实现的目标:- 我已经定义了用于执行每项任务的函数,以将邮件从简单文本发送到带有多部分电子邮件的高级邮件带有纯文本、html 格式并带有附件。

/*----------------------------------------------------------------------------------
   this function will take care of setting the mail depending users input what            he enters the message will be set up
 ----------------------------------------------------------------------------------*/
    public function set_mail_message($value)
{
$temp_message="";


// ----- detecting whether message submitted has html elements..]

if(strlen($value) != strlen(strip_tags($value)))
    {
    //...... message contains HTMLelements, so content type shud be 
    //....HTML type but for the compatiblity with older email clients
    //....we will set it to the both first html then plain text type..

    $temp_message.="This is a multipart message in MIME format";
    $temp_message.= "--".$this->boundary."\n";

    $temp_message.= "Content-type:text/html; charset=\"iso-8859-1\"\n";
    $temp_message.= "Content-Transfer-Encoding:7bit \n\n";

    $temp_message.= $value."\n";
    $temp_message.= "-- $this -> boundary \n";


//----------these codes from here-------------------------------

    $temp_message.= "Content-type:text/plain; charset=\"iso-8859-1\"\n";
    $temp_message.= "Content-Transfer-Encoding:7bit \n\n";
    $temp_message.= strip_tags($value)."\n";

 //--------------- upto HERE ARE OK................

//************ attach the attachment  prepared by function**********************/
        if($this->attachment_status == TRUE)
        {
        $temp_message.= "--".$this -> boundary."\n";

        $temp_message.= "Content-type: " . $Attachment_MIME_Type. "; \n name=\"$attachment_name\"\n";
        $temp_message.= "Content-Transfer_Encoding: base64\n\n";
        $temp_message.= $attachment_data."\n\n\";
        }       

    //----finishing the message configuration..........

    }
    else
    {
    // - ----content type is only PLAIN/TEXT---with or without attachmet-----
 //----------------BUT SAME CODES HERE FROM HERE :------------
    $temp_message.= '--'.$this->boundary.'\n';
    $temp_message.= 'Content-type:text/plain; charset=\"iso-8859-1\"\n'; 
    $temp_message.= 'Content-Transfer-Encoding:7bit \n\n';
    $temp_message.= $value.'\n';

//------------------UPTO HERE ARE SAME AS PREVIOUS BUT GIVING ERROR----------


//-------put attachment if set up by calling the set_mail_attachment function-------/
        if($this->attachment_status == TRUE)
        {
        $temp_message.= '--'.$this -> boundary.'\n';

        $temp_message.= 'Content-type: ' . $Attachment_MIME_Type. '; \n name=\'$attachment_name\'\n';
        $temp_message.= 'Content-Transfer_Encoding: base64\n\n';
        $temp_message.= $attachment_data.'\n\n';
        } 
        //----attachment has been put now close the boundary---

    $temp_message.= '--'.$this ->boundary.'--\n';
    }
$this ->message = $temp_message;

}



/*this function will take care of attchment and if this function is called and a 
atachment a selected only then the attachment part in set_mail_message() will be
defined*/   


   public function set_mail_attachment($value)
{

$attachment_data;

if(!empty($value) && file_exists($value))
    {
    $this -> attachment_status = TRUE;
//--here file type and file Name must be found somehow
//-- so that we can define them when seinding mail -------

    $Attachment_MIME_Type = 'image/jpeg';
    $attachment_name = 'attachment.jpeg';




    //------file must be read in binary format-------
    $file_resource = fopen($value,'rb')or die ('Error! There is an error in attachment');
    $attachment_data = fread($file_resource,filesize($value));
    fclose($file_resource);
    }

}

问题是:正如在代码中的两处注释的那样,代码是相同的,但在第二部分,相同的代码会产生解析错误,要解决这个问题,我需要用单引号更改双引号,但是当我调用这两个函数时这样做,如果我将参数作为双引号字符串传递,然后我得到相同的解析错误,如果我使用单引号而不是双引号,那么我得到另一个错误:UNexpected $end..

我多次遍历我的代码也试图在网上找到解决方案。但调试没有成功..如果您可以测试代码,请您自己也测试一下,以便可以追踪问题。

请帮助我..谢谢

4

2 回答 2

2

您是否注意到问题中代码块的颜色格式在此行之后变为红色?:$temp_message.= $attachment_data."\n\n\";

那是因为\"是一个转义字符,它实际上并没有关闭你的字符串。

它应该是:$temp_message.= $attachment_data."\n\n";

于 2012-09-10T18:13:42.163 回答
1

我认为你的问题是这里的这一行:

$temp_message.= $attachment_data."\n\n\";

字符串文字末尾的尾随反斜杠正在转义右双引号,这将导致代码中的解析错误。

于 2012-09-10T18:14:55.650 回答