2012-04-27 393 views
0

只是遷移網站到新的服務器。在遷移之後,php mail()發送以文本編碼且無法理解的電子郵件。在做了一些研究之後,似乎這可能是base64的一個問題?我不確定新安裝程序如何可能導致舊的以前工作的電子郵件模板不再起作用。舊的設置是Open Solaris,新的是Ubuntu 10.04。消息輸出:PHP郵件()Base64編碼發送電子郵件爲編碼文本

Content-Type: text/html; charset=utf-8 

MIME-Version: 1.0 
Mime-Version: 1.0 
Content-Transfer-Encoding: BASE64 
Message-Id: <[email protected]> 
Date: Fri, 27 Apr 2012 17:12:19 -0400 (EDT) 
X-pstn-dkim: 0 skipped:not-enabled 

PHA+RGVhciBNciBSaWNoYXJkIEVicmlnaHQsPC9wPgoKPHA+VGhhbmsgeW91IHZlcnkgbXVjaCBm 

b3Igc3VibWl0dGluZyBhIHNwZWNpYWwgYXBwbGljYXRpb24gdG8gSGFydmFyZApOYXRpb25hbCBN 

b2RlbCBVbml0ZWQgTmF0aW9ucyAyMDEzLjwvcD4KCjxwPldlIGFyZSB2ZXJ5IHBsZWFzZWQgdG8g 

YXNzaWduIHlvdSB0aGUgZm9sbG93aW5nIHBvc2l0aW9uOjxiciAvPgpBc3NpZ25tZW50OiBMYXJy 

eSBQYWdlIChDRU8pPGJyIC8+CkNvbW1pdHRlZTogWmFwYXRhJiMwMzk7cyBDYW1wLCAxOTExPC9w 

PgoKPHA+V2UgcmVjZWl2ZWQgYSBsYXJnZSBudW1iZXIgb2YgYXBwbGljYXRpb25zIHRoaXMgeWVh 

ciBhbmQgaGF2ZSBkb25lCm91ciB2ZXJ5IGJlc3QgdG8gYWNjb21tb2RhdGUgeW91ciBwcmVmZXJl 

bmNlcy4gUGxlYXNlIG5vdGUgdGhhdCBhbGwKYXNzaWdubWVudCBkZWNpc2lvbnMgYXJlIGZpbmFs 

LgoKVGhpcyBhc3NpZ25tZW50IGlzIG5vdyByZWZsZWN0ZWQgaW4geW91ciBhY2NvdW50IGluIG91 

ciA8YQpocmVmPSJodHRwOi8vbXVuYmFzZS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,第一次海報。希望更多地回饋給StackOverflow社區。

編輯: Yii的擴展,處理郵件發送(?)。

<?php 
/** 
* Email class file. 
* 
* @author Jonah Turnquist <[email protected]> 
* @link http://php-thoughts.cubedwater.com/ 
* @version 1.0 
*/ 
class Email extends CApplicationComponent { 
/** 
* @var string Type of email. Options include "text/html" and "text/plain" 
*/ 
public $type = 'text/html'; 
/** 
* @var string Receiver, or receivers of the mail. 
*/ 
public $to = null; 

/** 
* @var string Email subject 
*/ 
public $subject = ''; 

/** 
* @var string from address 
*/ 
public $from = null; 

/** 
* @var string Reply-to address 
*/ 
public $replyTo = null; 

/** 
* @var string Return-path address 
*/ 
public $returnPath = null; 

/** 
* @var string Carbon Copy 
* 
* List of email's that should receive a copy of the email. 
* The Recipient WILL be able to see this list 
*/ 
public $cc = null; 

/** 
* @var string Blind Carbon Copy 
* 
* List of email's that should receive a copy of the email. 
* The Recipient WILL NOT be able to see this list 
*/ 
public $bcc = null; 

/** 
* @var string Main content 
*/ 
public $message = ''; 

/** 
* @var string Delivery type. If set to 'php' it will use php's mail() function, and if set to 'debug' 
* it will not actually send it but output it to the screen 
*/ 
public $delivery = 'php'; 

/** 
* @var string language to encode the message in (eg "Japanese", "ja", "English", "en" and "uni" (UTF-8)) 
*/ 
public $language= 'uni'; 

/** 
* @var string the content-type of the email 
*/ 
public $contentType= 'utf-8'; 

/** 
* @var string The view to use as the content of the email, as an alternative to setting $this->message. 
* Must be located in application.views.email directory. This email object is availiable within the view 
* through $email, thus letting you define things such as the subject within the view (helps maintain 
* seperation of logic and output). 
*/ 
public $view = null; 

/** 
* @var array Variable to be sent to the view. 
*/ 
public $viewVars = null;  

/** 
* @var string The layout for the view to be imbedded in. Must be located in 
* application.views.email.layouts directory. Not required even if you are using a view 
*/ 
public $layout = null; 

/** 
* @var integer line length of email as per RFC2822 Section 2.1.1 
*/ 
public $lineLength = 70; 

public function __construct() { 
    Yii::setPathOfAlias('email', dirname(__FILE__).'/views'); 
} 

    /** 
    * Model United Nations Specific sending function. 
    */ 
    public function quick($subject, $view, $to, $info=null, $cc=null) { 
$this->subject = $subject; 
$this->from = 'ADDRESS REMOVED'; 
$this->to = $to; 
if ($cc) $this->cc = $cc; 
$this->bcc = 'ADDRESS REMOVED'; 
$this->view = $view; 
$this->send($info); 
    } 

/** 
* Sends email. 
* @param mixed the content of the email, or variables to be sent to the view. 
* If not set, it will use $this->message instead for the content of the email 
*/ 
public function send($arg1=null) { 
    if ($this->view !== null) { 
     if ($arg1 == null) 
      $vars = $this->viewVars; 
     else 
      $vars = $arg1; 

     $view = Yii::app()->controller->renderPartial('application.views.email.'.$this->view, array_merge($vars, array('email'=>$this)), true); 
     if ($this->layout === null) { 
      $message = $view; 
     } else { 
      $message = Yii::app()->controller->renderPartial('application.views.email.layouts.'.$this->layout, array('content'=>$view), true); 
     } 
    } else { 
     if ($arg1 === null) { 
      $message = $this->message; 
     } else { 
      $message = $arg1; 
     } 
    } 

    //process 'to' attribute 
    $to = $this->processAddresses($this->to); 
    return $this->mail($to, $this->subject, $message); 
} 

private function mail($to, $subject, $message) { 
    switch ($this->delivery) { 
     case 'php': 
      $message = wordwrap($message, $this->lineLength); 
    mb_internal_encoding("UTF-8"); 
      mb_language($this->language); 
      return mb_send_mail($to, $subject, $message, implode("\r\n", $this->createHeaders()), '[email protected]'); 
     case 'debug': 
      $debug = Yii::app()->controller->renderPartial('email.debug', 
        array_merge(compact('to', 'subject', 'message'), array('headers'=>$this->createHeaders())), 
        true); 
      Yii::app()->user->setFlash('email', $debug); 
      break; 
    } 
} 
private function createHeaders() { 
    $headers = array(); 

    //maps class variable names to header names 
    $map = array(
     'from' => 'From', 
     'cc' => 'Cc', 
     'bcc' => 'Bcc', 
     'replyTo' => 'Reply-To', 
     'returnPath' => 'Return-Path', 
    ); 
    foreach ($map as $key => $value) { 
     if (isset($this->$key)) 
      $headers[] = "$value: {$this->processAddresses($this->$key)}"; 
    } 
    $headers[] = "Content-Type: {$this->type}; charset=".$this->contentType; 
    $headers[] = "MIME-Version: 1.0"; 

    return $headers; 
} 
private function processAddresses($addresses) { 
    return (is_array($addresses)) ? implode(', ', $addresses) : $addresses; 
} 
} 
+0

,如果你寫在小寫的「BASE64」字,並把它僅僅是消息之前會發生什麼? – 2012-04-27 22:25:48

+0

請看我們的PHP代碼嗎?消息本身是編碼字符串一個有效的base64,我想@tpaksu可能將要發生什麼,在標頭值的'BASE64'的外殼,它通常是'base64' - 你的郵件客戶端看到上面的整個文本或者只是無意義的文字?如果你也看到標題,我想你可能會在這裏泄漏一些額外的換行符。 – DaveRandom 2012-04-27 22:34:15

+0

謝謝!我似乎無法找到設置標題值的位置。真相被告知,我沒有編寫這個基於Yii的web應用程序的代碼,我現在正在幫助遷移,並且不知道原始工程師做了什麼。我已經編輯了與電子郵件腳本可能相關的配置文件的問題。在'內容Type'和'MIME-Version'標題行之間 – 2012-04-29 04:17:45

回答

0

原來,額外的線返回是罪魁禍首。原始(車版本)消息報頭被摘錄如下:

X-PHP-Originating-Script: 0:Email.php 
From: Harvard National Model United Nations 2013 <[email protected]> 

Content-Type: text/html; charset=UTF-8 

Mime-Version: 1.0 

Content-Transfer-Encoding: BASE64 

,並通過在擴展控制的內爆命令引起的:

private function mail($to, $subject, $message) { 
switch ($this->delivery) { 
    case 'php': 
     $message = wordwrap($message, $this->lineLength); 
mb_internal_encoding("UTF-8"); 
     mb_language($this->language); 
     return mb_send_mail($to, $subject, $message, implode("\r\n", $this->createHeaders()), '[email protected]'); 
    case 'debug': 
     $debug = Yii::app()->controller->renderPartial('email.debug', 
       array_merge(compact('to', 'subject', 'message'), array('headers'=>$this->createHeaders())), 
       true); 
     Yii::app()->user->setFlash('email', $debug); 
     break; 
} 

原來它需要的implode("\n", $this->createHeaders())參數代替implode("\r\n", $this->createHeaders()) 可能與新版本的PHP或新操作系統有關?感謝大家的幫助!