快速赶上:由于我最近发现的内容的性质。我已经在另一个类别中重新创建了此问题,因为这似乎不是Mailgun问题。这个问题可以在这里找到:
PHP 7.2 CURLFile Gives "Invalid Filename" Warning
我知道SDK,而Mailgun并不提供它之外的支持,这就是为什么我试图在这里为我正在研究的轻量级实现提供支持。它实际上已经完成并且可以正常工作,直到我决定使用附件扩展它,并且仍然可以正常工作,只是没有附件被添加到电子邮件中。我已经回顾了一些以前的问题,但根据提供的解决方案,我似乎无法获得附件的支持。
信息
$this
的引用。所有变量均正确加载。attachment
键-结果是一样的。代码块
$curl = curl_init();
$log->AddStep('Mailgun', 'API send request starting...');
$postUrl = 'https://' . $this->host . self::API_URL_BASE . '/' . $this->domain . '/messages';
$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');
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]));
}
} else {
\D3DevelForms\Models\Error::CreateAndSaveSystemError(
$plugin,
\D3DevelForms\Common::ERROR_WARNING,
'PHP 5.5 or newer required for Mailgun Attachments',
\D3DevelForms\Models\Error::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);
卷曲选项($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 <devtester@devtest.com>
[to] => devemail@gmail.com
[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
)
)
)
返回的卷曲信息($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
)
Mailgun API响应
{
"id": "<AA.BB.CC@devtester.devtest.com>",
"message": "Queued. Thank you."
}
Mailgun日志
{
"tags": [],
"envelope": {
"sender": "devtester@devtest.com",
"transport": "smtp",
"targets": "devemail@gmail.com"
},
"storage": {
"url": "https://sw.api.mailgun.net/v3/domains/devtester.devtest.com/messages/KK",
"key": "KK"
},
"log-level": "info",
"id": "II",
"campaigns": [],
"method": "http",
"user-variables": {},
"flags": {
"is-routed": false,
"is-authenticated": true,
"is-system-test": false,
"is-test-mode": false
},
"recipient-domain": "gmail.com",
"timestamp": 1550085991.344005,
"message": {
"headers": {
"to": "devemail@gmail.com",
"message-id": "AA.BB.CC@devtester.devtest.com",
"from": "Dev Tester <devtester@devtest.com>",
"subject": "Form Summary"
},
"attachments": [],
"size": 945
},
"recipient": "devemail@gmail.com",
"event": "accepted"
}
我已经为此苦苦挣扎了几天,目的是避免出现这个问题,这仅仅是因为预期的不使用SDK的阻力,我认为是最后的选择,因为这一切都可以进行,而无需加载整个图书馆。
你们是否能看到阻止此代码通过PHP + cURL发送附件的任何内容?
已审查的相关问题:
更新:从命令行使用cURL进行测试时,它确实可以按预期工作,包括当我以apache
进程运行它时。
sudo -u apache curl -s --user 'api:APIKEY' \
https://api.mailgun.net/v3/devtester.devtest.com/messages \
-F from='Dev Tester <devtest@devtester.devtest.com>' \
-F to='devtester@gmail.com' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
-F attachment=@/var/www/path_to/my_file.csv
{
"id": "<AA.BB.CC@devtester.devtest.com>",
"message": "Queued. Thank you."
}
更新(解决问题):
我在Apache日志中收到一个PHP警告,显示如下:
“ PHP警告:curl_setopt_array():密钥附件的无效文件名[1]”
这很棘手,因为我已经确认了以下内容: