通过CakePHP中的MailGun发送邮件

时间:2013-08-05 18:21:09

标签: email cakephp mailgun

我在Rackspace上运行一个云应用程序(使用CakePHP),我想使用cakephp发送电子邮件。 我用过这个:https://github.com/kochb/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'));

我编辑了插入MailGun API凭据等的Config / Email.php文件。

可能发生什么事?你能找出为什么会这样吗?

提前致谢!

1 个答案:

答案 0 :(得分: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);
    }