Skip to content

Commit

Permalink
Use CURLFile to attach files if possible. Closes #12.
Browse files Browse the repository at this point in the history
  • Loading branch information
baibaratsky committed Aug 15, 2015
1 parent dfbd327 commit 916dde6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 13 additions & 2 deletions MailgunMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class MailgunMessage implements MailgunObject
const CLICKS_TRACKING_ENABLED = 1;
const CLICKS_TRACKING_HTML_ONLY = 2;

protected static $_legacyAttachMode;

private $_from;
private $_to = array();
private $_cc = array();
Expand Down Expand Up @@ -35,6 +37,10 @@ class MailgunMessage implements MailgunObject

public function __construct(MailgunApi $api)
{
if (self::$_legacyAttachMode === null) {
self::$_legacyAttachMode = !class_exists('CURLFile', false);
}

$this->_api = $api;
$this->_from = $api->getFrom();
$this->_tags = $api->getTags();
Expand Down Expand Up @@ -477,11 +483,11 @@ public function getPostData()
}

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

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

foreach ($this->_tags as $number => $tag) {
Expand Down Expand Up @@ -570,4 +576,9 @@ private function _formatAddresses(array $addresses)
}
return implode(', ', $formattedAddresses);
}

private function _attach($attachment)
{
return self::$_legacyAttachMode ? '@' . $attachment : new CURLFile($attachment);
}
}
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "baibaratsky/php-mailgun",
"description": "Mailgun API PHP library and Yii extension",
"keywords": ["mailgun", "mail", "email", "mailing lists", "yii"],
"description": "Mailgun API PHP library and Yii extension (as well as Yii2)",
"keywords": ["mailgun", "mail", "email", "mailing lists", "yii", "yii2"],
"license": "BSD-3-Clause",
"homepage": "http://github.com/baibaratsky/php-mailgun",
"authors": [
Expand All @@ -14,6 +14,10 @@
"php": ">=5.2",
"ext-curl": "*"
},
"require-dev": {
"yiisoft/yii": "*",
"yiisoft/yii2": "*"
},
"autoload": {
"classmap": [""]
}
Expand Down

0 comments on commit 916dde6

Please sign in to comment.