2013-08-05 66 views
1

我在Rackspace上運行一個雲應用(使用CakePHP),我想用cakephp發送郵件。 我用這個:https://github.com/kochb/cakephp-mailgun 但它返回我一個在CakePHP中通過MailGun發送郵件

"Could not send email. 

Error: An Internal Error Has Occurred." 

錯誤。 我嘗試發送電子郵件的方法是使用下面的代碼:

$Email = new CakeEmail(); 


      $from = $this->request->data['Mail']['from']; 
      $to = ($this->request->data['Mail']['to']); 
      $subject = $this->request->data['Mail']['subject']; 
      $message = $this->request->data['Mail']['message']; 

       $Email->sender($from, 'TestName'); 
       $Email->from($from) 
        ->bcc($to) 
        ->replyTo($from) 
        ->subject($subject) 
        ->send($message); 



        $this->Session->setFlash('On the way to recipient'); 
        $this->redirect(array('action' => 'index')); 

我已經編輯配置/ Email.php文件中插入MailGun API憑證等

什麼可能是怎麼回事?你能找出爲什麼會發生這種情況嗎?

在此先感謝!

+1

你可以試試 「從」 規定, 「對」, 「主題」 等手?我從字面上複製了您的示例,但設置了這些參數,並且發送的信息沒有問題。這是[Gist](https://gist.github.com/travelton/6160051)。 –

回答

1

(我有你同樣的錯誤)

BasicTransport沒有正確的「前處理」,也沒有相應的響應處理。

我複製了CurlTransport的功能,現在它適用於我。

具體而言,我們需要:

$post = array(); 
    $post_preprocess = array_merge(
     $email->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject')), 
     array(
      'text' => $email->message(CakeEmail::MESSAGE_TEXT), 
      'html' => $email->message(CakeEmail::MESSAGE_HTML) 
     ) 
    ); 
    foreach ($post_preprocess as $k => $v) { 
     if (! empty($v)) { 
      $post[strtolower($k)] = $v; 
     } 
    } 

然後:

$response = $http->post($url, $post, $request); 
    if ($response === false) { 
     throw new SocketException("Mailgun BasicTransport error, no response", 500); 
    } 

    $http_status = $response->code; 
    if ($http_status != 200) { 
     throw new SocketException("Mailgun request failed. Status: $http_status, Response: {$response->body}", 500); 
    }