2013-08-05 64 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); 
    }