Reputation: 1
I have an app built with CakePHP 2.9 and am trying to send emails using 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.
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
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
<?php
/**
* Mailgun curl class
*
* Enables sending of email over mailgun via curl
*
* Licensed under The MIT License
*
* @author Brad Koch <[email protected]>
* @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())
);
}
}
Upvotes: 0
Views: 219
Reputation: 1
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 <[email protected]>
* @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())
);
}
}
Upvotes: 0