2015-10-08 74 views
0

我創建了一個類來簡化mail()的使用(可以添加附件,在文本和html模式之間切換等)。php郵件在某些服務器上發送空白內容

我目前正面臨着這樣的問題:當我試圖從我的本地服務器(XAMPP)發送郵件

  • ,一切工作正常。
  • 當我嘗試從真實 apache服務器發送郵件時,郵件被正確發送,但的內容爲空。儘管(發件人,收件人,CC,...)其他一切都很好。

從關於這類問題的SO問題中,我認爲它與CRLF有關,但我無法確切地找到問題出在哪裏(也沒有任何SO問題有幫助)。

這是我如何使用它的例子:

<?php 
    $mail = new Mail(); 
    $mail->from('** adress **', '** name **'); 
    $mail->addTo($user->mail); 
    $mail->addCC('** CC adress **', '** name **'); 

    $mail->html = true; 
    $mail->subject = '** subject of the mail **'; 
    $mail->content = '** HTML code **'; // This HTML is valid 
    $mail->send(); 

這裏是類:

<?php 
define('CRLF', "\r\n"); 

// This is used to send mail 
class Mail extends Controller 
{ 
    // Boundary 
    private $boundary = null; 

    // Recipients, RFC 2822 
    public $to = []; 
    // Subject of the mail, RFC 2047 
    public $subject = ''; 

    // Content of the mail 
    public $message = ''; 
    // Content is HTML ? 
    public $html = false; 
    // Attachments 
    public $attachments = []; 

    // Additional headers 
    public $headers = []; 

    public function __construct() 
    { 
     $this->boundary = '_'.md5(uniqid(rand())).'_'; 
     $this->headers = [ 
      'MIME-Version' => '1.0', 
      'Content-Type' => 'multipart/mixed; boundary="'.$this->boundary.'"' 
     ]; 
    } 

    public function addTo($mail, $name = null) 
    { 
     $this->to[] = ($name ? $name.' ' : '').'<'.$mail.'>'; 
     return $this; 
    } 

    public function from($mail, $name = null) 
    { 
     $this->headers['From'] = ($name ? $name.' ' : '').'<'.$mail.'>'; 
     return $this; 
    } 

    public function replyTo($mail, $name = null) 
    { 
     $this->headers['Reply-to'] = ($name ? $name.' ' : '').'<'.$mail.'>'; 
     return $this; 
    } 

    public function addCC($mail, $name = null) 
    { 
     $this->headers['Cc'][] = ($name ? $name.' ' : '').'<'.$mail.'>'; 
     return $this; 
    } 

    public function addBC($mail, $name = null) 
    { 
     $this->headers['Bcc'][] = ($name ? $name.' ' : '').'<'.$mail.'>'; 
     return $this; 
    } 

    public function addAttachment($fileName, $name = null) 
    { 
     $this->attachments[] = [$fileName, ($name === null ? pathinfo($fileName, PATHINFO_FILENAME) : $name)]; 
     return $this; 
    } 

    public function send() 
    { 
     // Add main content 
     $fullMessage = '--'.$this->boundary.CRLF. 
      'Content-Type: '.($this->html ? 'text/html' : 'text/plain').'; charset=UTF-8'.CRLF.CRLF. 
      $this->content; 

     // Attachments 
     foreach ($this->attachments as $attachment) 
     { 
      if (!is_readable($attachment[0])) 
       throw new Exception('Cannot read file "'.$attachment[0].'"'); 
      $fullMessage .= CRLF.CRLF.'--'.$this->boundary.$thids->nl. 
       'Content-Type: '.mime_content_type($attachment[0]).'; name="'.$attachment[1].'"'.CRLF. 
       'Content-Transfer-Encoding: base64'.CRLF. 
       'Content-Disposition: attachment; filename="'.$attachment[0].'"'.CRLF.CRLF. 
       chunk_split(base64_encode(file_get_contents($attachment[0]))).CRLF; 
     } 

     // Adding the headers 
     $fullHeaders = ''; 
     foreach ($this->headers as $name => $content) 
     { 
      if (is_array($content)) 
       $fullHeaders .= $name.': '.implode(', ', $content).CRLF; 
      else 
       $fullHeaders .= $name.': '.$content.CRLF; 
     } 

     return mail(implode(',', $this->to), utf8_decode($this->subject), $fullMessage, $fullHeaders); 
    } 
} 
+0

考慮檢查您的測試和生產環境配置。 也許在PHP構建中有所不同 –

回答

0

問題從\r\n\n來了。 我解決了這個問題,將郵件內容中的所有\r\n替換爲\n(不在標頭中)。

相關問題