Skip to content

Commit 916dde6

Browse files
committed
Use CURLFile to attach files if possible. Closes #12.
1 parent dfbd327 commit 916dde6

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

MailgunMessage.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class MailgunMessage implements MailgunObject
77
const CLICKS_TRACKING_ENABLED = 1;
88
const CLICKS_TRACKING_HTML_ONLY = 2;
99

10+
protected static $_legacyAttachMode;
11+
1012
private $_from;
1113
private $_to = array();
1214
private $_cc = array();
@@ -35,6 +37,10 @@ class MailgunMessage implements MailgunObject
3537

3638
public function __construct(MailgunApi $api)
3739
{
40+
if (self::$_legacyAttachMode === null) {
41+
self::$_legacyAttachMode = !class_exists('CURLFile', false);
42+
}
43+
3844
$this->_api = $api;
3945
$this->_from = $api->getFrom();
4046
$this->_tags = $api->getTags();
@@ -477,11 +483,11 @@ public function getPostData()
477483
}
478484

479485
foreach ($this->_attachments as $number => $attachment) {
480-
$data['attachment[' . ($number + 1) . ']'] = '@' . $attachment;
486+
$data['attachment[' . ($number + 1) . ']'] = $this->_attach($attachment);
481487
}
482488

483489
foreach ($this->_inline as $number => $attachment) {
484-
$data['inline[' . ($number + 1) . ']'] = '@' . $attachment;
490+
$data['inline[' . ($number + 1) . ']'] = $this->_attach($attachment);
485491
}
486492

487493
foreach ($this->_tags as $number => $tag) {
@@ -570,4 +576,9 @@ private function _formatAddresses(array $addresses)
570576
}
571577
return implode(', ', $formattedAddresses);
572578
}
579+
580+
private function _attach($attachment)
581+
{
582+
return self::$_legacyAttachMode ? '@' . $attachment : new CURLFile($attachment);
583+
}
573584
}

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "baibaratsky/php-mailgun",
3-
"description": "Mailgun API PHP library and Yii extension",
4-
"keywords": ["mailgun", "mail", "email", "mailing lists", "yii"],
3+
"description": "Mailgun API PHP library and Yii extension (as well as Yii2)",
4+
"keywords": ["mailgun", "mail", "email", "mailing lists", "yii", "yii2"],
55
"license": "BSD-3-Clause",
66
"homepage": "http://github.com/baibaratsky/php-mailgun",
77
"authors": [
@@ -14,6 +14,10 @@
1414
"php": ">=5.2",
1515
"ext-curl": "*"
1616
},
17+
"require-dev": {
18+
"yiisoft/yii": "*",
19+
"yiisoft/yii2": "*"
20+
},
1721
"autoload": {
1822
"classmap": [""]
1923
}

0 commit comments

Comments
 (0)