2013-10-31 76 views
2

時不能「從」字段設置我嘗試使用ZF2Zend框架2 - 發送電子郵件

$message = new \Zend\Mail\Message(); 
    $message->setBody('This is the body'); 
    $message->setFrom('[email protected]'); 
    $message->addTo('[email protected]'); 
    $message->setSubject('Test subject'); 

    $smtpOptions = new \Zend\Mail\Transport\SmtpOptions(); 

    $smtpOptions->setHost('smtp.gmail.com') 
     ->setConnectionClass('login') 
     ->setName('smtp.gmail.com') 
     ->setConnectionConfig(array(
      'username' => '[email protected]', 
      'password' => 'mypassword', 
      'ssl' => 'tls', 
      'port' => '123', 
     )); 

    $transport = new \Zend\Mail\Transport\Smtp($smtpOptions); 
    $transport->send($message); 

我希望發件人是「[email protected]」,但它總是'發送電子郵件[email protected]'

我該如何解決這個問題?我做錯了什麼?

編輯:

我也具有相同的結果

+0

在「admin @ mydomain.com.com」中搜索您所有的項目 - 很可能你不會找到它。如果是這樣,那麼你的服務器配置設置爲在發送之前簡單地覆蓋特定的郵件頭。 – Sam

+0

它只在一個地方,謝謝 – Thermech

回答

0

綜觀源代碼\ Zend的\郵件\傳輸\的smtp\ Zend的\郵件\試圖與setSender()消息在我看來,你做的是正確的類; 發送函數(Smtp類)首先連接到smtp服務器,然後準備消息。其中一項任務是從地址獲取,這是導致您遇到問題的一個地址。這是代碼:

protected function prepareFromAddress(Message $message) 
{ 
    $sender = $message->getSender(); 
    if ($sender instanceof Address\AddressInterface) { 
     return $sender->getEmail(); 
    } 

    $from = $message->getFrom(); 
    if (!count($from)) { // Per RFC 2822 3.6 
     throw new Exception\RuntimeException(sprintf(
      '%s transport expects either a Sender or at least one From address in the Message; none provided', 
      __CLASS__ 
     )); 
    } 

    $from->rewind(); 
    $sender = $from->current(); 
    return $sender->getEmail(); 
} 

我沒有通過我自己的測試,您可以檢查它從$ MESSAGE-> GETFROM()得到;?或者,也許它從$ sender-> getEmail();

有一兩件事你可以嘗試是使用setSender()代替setFrom()方法在定義消息設置「[email protected]」地址。我不太瞭解他們之間的區別,但看着prepareFromAddress函數,似乎你可以用它來放置地址地址。

+0

我已經嘗試過,使用setSender()失敗。如果發件人已設置,則以發件人爲準,否則爲發件人。我檢查標題並將數據正確發送到gmail服務器 – Thermech

相關問題