2012-09-10 31 views
-3

任何人都可以帮助我理解代码中发生的事情吗?无论是编程错误还是PHP引擎造成的错误? 以下是我想提出来发送电子邮件非常伊斯利类的代码..
我想实现: -我已经定义函数执行每项任务needend从一个简单的文本发送邮件到带有纯文本的多部分电子邮件的预发邮件,html格式和附件。我正面临一个奇怪的关于PHP字符串的错误问题

/*---------------------------------------------------------------------------------- 
    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报价单引号,但这样做我什么时候我调用这两个函数,如果我将参数传递为双引号字符串,然后相同的解析错误我得到,如果我使用单引号,而不是双重然后我得到另一个错误:意想不到的结束..

我遍历了我的代码很多时候也试图在网上找到解决方案。但没有得到成功的调试这个.. 如果你可以测试的代码,请自己测试一下,这样的问题可以追查。

请帮助我..我 在

+0

您是否知道现有的类(phpmailer,swiftmailer)可以省去繁琐的手动构建多部分消息? – mario

+0

是的马里奥我非常了解这些课程......但是当谈到理解和学习编程时,我会同意 - 有一个特定问题的数百万解决方案,但是我认为学习者Isn'这是非常好的尝试实现你想要的任务自己..它有助于在不理想的概念。它感觉非常好.. –

回答

2

感谢你有没有注意到如何的问题,该行后的代码块变为红色的颜色格式?:$temp_message.= $attachment_data."\n\n\";

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

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

+0

谢谢亲爱的!我试着用这个解决方案。 –

+0

谢谢亲爱的@克里斯蒂安@ –

+0

谢谢亲爱的@克里斯蒂安@它解决了我面临的问题。但现在我面临着另一个问题,即发送现在发送的邮件中的邮件而不是邮件的内容(文本por html),我收到的邮件包含内容类型声明,内容传输编码以及我拥有的所有内容我的代码定义邮件味精.........我要求plz如果你可以帮助我做什么sud ...... –

1

我觉得你的问题是这条线的位置:

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

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

+0

谢谢你.. –