I am having an issue using the CURLFile object (curl_file_create
). I have tried a number of approaches, but no matter what I try, I end up getting a PHP Warning in the log files and the file is omitted from my cURL call.
First, some information:
- PHP 7.2
- Files have been confirmed that they exist and are readable by PHP/Apache
- php-curl and related php libraries are up-to-date
- This is a snippet from within an object, thus the references to
$this
. All variables are loading correctly.
- This is a continuation of another question I started that, when narrowed, seems to be unrelated to the primary original topic (mailgun). It can be referenced at this URL if you like, but I intend to close that question shortly. Mailgun Attachments with PHP cURL - No SDK
- Note in the code block where I have commented out several other variants of trying to add the file -- all of them create the same warning/result
Code Block
$curl = curl_init();
$curlOpts = array(
CURLOPT_POST => 1,
CURLOPT_URL => $postUrl,
CURLOPT_TIMEOUT => 20,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => 'api:' . $this->apiKey
);
$postFields = array(
'from' => $email->from,
'to' => $email->to,
'subject' => $email->subject
);
if (strlen($email->cc) > 0) {
$postFields['cc'] = $email->cc;
}
if (strlen($email->bcc) > 0) {
$postFields['bcc'] = $email->bcc;
}
if (strlen($email->html) > 0) {
$postFields['html'] = $email->html;
} else {
$postFields['text'] = $email->text;
}
if (count($email->attachments) > 0) {
// Curl attachments for < PHP5.5 not supported
if (function_exists('curl_file_create')) {
$curlOpts[CURLOPT_SAFE_UPLOAD] = 1; // for < PHP 7
//$curlOpts[CURLOPT_HTTPHEADER] = array('Content-Type: multipart/form-data');
//$postFields['attachment'] = curl_file_create($email->attachments[0]);
for ($i = 1; $i <= count($email->attachments); $i++) {
$postFields['attachment[' . $i . ']'] = curl_file_create($email->attachments[$i - 1], 'text/csv', basename($email->attachments[$i - 1]));
//$postFields['attachment[' . $i . ']'] = curl_file_create('/var/www/sites/domain/contact.csv', 'text/csv', 'contact.csv');
//$postFields['attachment[' . $i . ']'] = curl_file_create('test.txt', 'text/plain', 'test.txt');
//$postFields['attachment[' . $i . ']'] = curl_file_create(realpath('test.txt'), 'text/plain', 'test.txt');
//$postFields['attachment[' . $i . ']'] = new CURLFile($email->attachments[$i - 1]);
}
} else {
D3DevelFormsModelsError::CreateAndSaveSystemError(
$plugin,
D3DevelFormsCommon::ERROR_WARNING,
'PHP 5.5 or newer required for Mailgun Attachments',
D3DevelFormsModelsError::ERROR_CODE_API_MAILGUN_LOCAL_ERROR,
'You are using an outdated version of PHP. Email attachments via Mailgun will be ignored.');
}
}
$curlOpts[CURLOPT_POSTFIELDS] = $postFields;
$log->UpdateDebugLog('Mailgun API Options', $curlOpts);
curl_setopt_array($curl, $curlOpts);
$curl_response = curl_exec($curl);
$info = curl_getinfo($curl);
Curl Options ($curlOpts
)
Array
(
[47] => 1
[10002] => https://api.mailgun.net/v3/devtester.devtest.com/messages
[13] => 20
[19913] => 1
[107] => 1
[10005] => api:APIKEY
[-1] => 1
[10015] => Array
(
[from] => Dev Tester <[email protected]>
[to] => [email protected]
[subject] => Form Summary
[text] => My Text Content
[attachment[1]] => CURLFile Object
(
[name] => /var/www/path_to/my_file.csv
[mime] => text/csv
[postname] => my_file.csv
)
)
)
Curl Info Returned ($info
)
Array
(
[url] => https://api.mailgun.net/v3/devtester.devtest.com/messages
[content_type] => application/json
[http_code] => 200
[header_size] => 388
[request_size] => 312
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.503718
[namelookup_time] => 0.004273
[connect_time] => 0.0932
[pretransfer_time] => 0.279756
[size_upload] => 1021
[size_download] => 105
[speed_download] => 208
[speed_upload] => 2026
[download_content_length] => 105
[upload_content_length] => 1021
[starttransfer_time] => 0.368725
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => Y.Y.Y.Y
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => X.X.X.X
[local_port] => 38636
)
Update: When testing with cURL from command line, it does work as intended including when I run it as an the apache
process.
sudo -u apache curl -s --user 'api:APIKEY'
https://api.mailgun.net/v3/devtester.devtest.com/messages
-F from='Dev Tester <[email protected]>'
-F to='[email protected]'
-F subject='Hello'
-F text='Testing some Mailgun awesomness!'
-F attachment=@/var/www/path_to/my_file.csv
{
"id": "<[email protected]>",
"message": "Queued. Thank you."
}
I am getting a PHP warning in the Apache logs, that appears as follows:
"PHP Warning: curl_setopt_array(): Invalid filename for key attachment[1]"
This is tricky because I have confirmed the following:
- The file exists
- The file is readable by Apache
- The file path does not include any characters outside of letters, numbers, slashes and hyphens
- Because the file is generated within the same thread, I have tried referencing a static file, but the result is the same.
See Question&Answers more detail:
os