简体   繁体   English

CakePHP Mailgun带有附件的电子邮件

[英]CakePHP Mailgun Email with Attachment

I have an app built with CakePHP 2.9 and am trying to send emails using mailgun. 我有一个使用CakePHP 2.9构建的应用程序,正在尝试使用mailgun发送电子邮件。 The emails get delivered with all of the needed content however attachments dont show up. 电子邮件将包含所有必需的内容,但是附件不会显示。 Mailgun log looks good except for the attachment array being empty. Mailgun日志看起来不错,除了附件数组为空。

I was trying to send a dynamic file (pdf) however for testing purposes I've switched to a local png file and it still isn't working 我试图发送动态文件(pdf),但是出于测试目的,我已切换到本地png文件,但仍然无法正常工作

I've been trying a few ways to get the attachment to be added but havent been able to here is the code 我一直在尝试一些方法来添加附件,但是还不能在这里找到代码

Email code 电邮代码

$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();

Here is the CurlTransport.php 这是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())
        );
    }
}

Here is the updated code to work with attachments 这是与附件一起使用的更新代码

Basically changed the line From 基本上改变了线路

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

To

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

Full code 完整代码

<?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())
        );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM