2012-09-10 32 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

謝謝你.. –