【问题标题】:CakePHP Mailgun Email with Attachment带附件的 CakePHP Mailgun 电子邮件
【发布时间】:2019-07-22 16:28:06
【问题描述】:

我有一个使用 CakePHP 2.9 构建的应用程序,我正在尝试使用 mailgun 发送电子邮件。电子邮件随所有需要的内容一起发送,但附件不显示。 Mailgun 日志看起来不错,只是附件数组为空。

我试图发送一个动态文件 (pdf),但出于测试目的,我已切换到本地 png 文件,但它仍然无法正常工作

我一直在尝试几种方法来添加附件,但无法找到代码

电子邮件代码

$Email = new CakeEmail('mailgun');
$Email->template('pdf');
$Email->emailFormat('html');
$Email->to($email);
$Email->subject($configdata['Config']['accountstatementsubject']);
$Path = WWW_ROOT."pdf/";
$fileName = 'Account_'.$usersGenrate['User']['fname'].'.pdf';

$Email->attachments(WWW_ROOT.'img/bg.png');
$Email->viewVars(array('emailcontent' => $configdata['Config']['accountstatementcontent'],
                        'user' => $usersGenrate['User']));
$Email->send();

这里是 CurlTransport.php

<?php
/**
 * Mailgun curl class
 *
 * Enables sending of email over mailgun via curl
 *
 * Licensed under The MIT License
 * 
 * @author Brad Koch <bradkoch2007@gmail.com>
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class CurlTransport extends AbstractTransport {
/**
 * Configurations
 *
 * @var array
 */
    protected $_config = array();
/**
 * Send mail
 *
 * @params CakeEmail $email
 * @return array
 */
    public function send(CakeEmail $email) {
        $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;
            }
        }

        if ($attachments = $email->attachments()) {
            $i = 1;
            foreach ($attachments as $attachment) {
                $post['attachment[' . $i . ']'] = "@" . $attachment["file"];
                $i++;
            }
        }
        $ch = curl_init('https://api.mailgun.net/v2/' . $this->_config['mailgun_domain'] . '/messages');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $this->_config['api_key']);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $response = curl_exec($ch);
        if ($response === false) {
            throw new SocketException("Curl had an error.  Message: " . curl_error($ch), 500);
        }
        $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($http_status != 200) {
            throw new SocketException("Mailgun request failed.  Status: $http_status, Response: $response", 500);
        }
        curl_close($ch);
        return array(
            'headers' => $this->_headersToString($email->getHeaders(), PHP_EOL),
            'message' => implode(PHP_EOL, $email->message())
        );
    }
}

【问题讨论】:

  • 您是否检查过您的传输是否创建了预期的发布数据?还有你的确切 PHP 和 CakePHP 版本是什么?
  • 道歉,忘记指定它的 CakePHP 2.9 - mailgun 日志说它正在接收除附件之外的所有帖子数据:/
  • 我明白,但我的意思是在实际发送之前检查传输中的数据。还有你的 PHP 版本是什么?
  • 最新的 PHP 版本,实际上我没想过仔细检查你的数据是否正确,仔细检查让我意识到它没有正确完成。我对这一切还是新手,所以我并不总是想到所有事情这是我用来查看正在制作的内容的链接ptsv2.com

标签: php curl cakephp mailgun


【解决方案1】:

这是处理附件的更新代码

基本上换行了 来自

$post['attachment[' . $i . ']'] = "@" . $attachment["file"];

$post['attachment[' . $i . ']'] = curl_file_create($attachment["file"]);

完整代码

<?php
/**
 * Mailgun curl class
 *
 * Enables sending of email over mailgun via curl
 *
 * Licensed under The MIT License
 * 
 * @author Brad Koch <bradkoch2007@gmail.com>
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
class CurlTransport extends AbstractTransport {
/**
 * Configurations
 *
 * @var array
 */
    protected $_config = array();
/**
 * Send mail
 *
 * @params CakeEmail $email
 * @return array
 */
    public function send(CakeEmail $email) {
        $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;
            }
        }
        if ($attachments = $email->attachments()) {
            $i = 1;
            foreach ($attachments as $attachment) {
                $post['attachment[' . $i . ']'] = curl_file_create($attachment["file"]);
                $i++;
            }
        }

        /*$ch = curl_init('https://ptsv2.com/t/82b3y-1563814313/post');*/
        $ch = curl_init('https://api.mailgun.net/v2/' . $this->_config['mailgun_domain'] . '/messages');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $this->_config['api_key']);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $response = curl_exec($ch);
        if ($response === false) {
            throw new SocketException("Curl had an error.  Message: " . curl_error($ch), 500);
        }
        $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($http_status != 200) {
            throw new SocketException("Mailgun request failed.  Status: $http_status, Response: $response", 500);
        }
        curl_close($ch);
        return array(
            'headers' => $this->_headersToString($email->getHeaders(), PHP_EOL),
            'message' => implode(PHP_EOL, $email->message())
        );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    相关资源
    最近更新 更多