关于php Strings,我正面临一个奇怪的错误问题

时间:2012-09-10 18:09:17

标签: string email php

任何人都可以帮助我理解我的代码中发生的事情吗?它是编程中的错误还是由于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);
    }

}

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

我遍历我的代码很多时候试图在网上找到解决方案。但没有成功调试这个.. 如果您可以测试代码,请自行测试,以便可以跟踪问题。

请帮帮我.. 我的感谢

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

我认为你的问题就在这里:

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

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