我正在 Yeti CRM 中使用一个表单,该表单允许用户查看文档,然后让他们选择通过电子邮件发送所述文档。它正在使用 PHPMailer,但我无法让它附加文档。所有其他字段都响应编辑(收件人、发件人、主题、正文),但没有附加任何内容。错误日志不会引发错误。
我尝试直接链接一个我知道存在的文件,但仍然没有成功。任何人都可以在下面的代码中看到我实际上没有附加文档的地方吗?
这是PHP函数
public function process(\App\Request $request)
{
$moduleName = $request->getModule();
$recordId = $request->getInteger('record');
$documentRecordModel = Vtiger_Record_Model::getInstanceById($recordId, $moduleName);
$currentUser = Users_Record_Model::getCurrentUserModel();
$mails = $request->get('to');
$message = $request->get('message');
$from['email'] = $currentUser->get('email1');
$name = $currentUser->get('first_name') . " " . $currentUser->get('last_name');
$from['name'] = $name;
$file = $documentRecordModel->get('filename');
$title = $documentRecordModel->get('notes_title') . substr($file, strrpos($file, "."));
if (strpos($file, "\\\\") !== false) {
$file = trim($file);
$file = str_replace('\\\\', '\\', $file);
$file = str_replace('\\', '/', $file);
$file = str_replace(' ', '%20', $file);
$file = ROOT_DIRECTORY . DIRECTORY_SEPARATOR .
"public_html" . DIRECTORY_SEPARATOR .
"external" .
$file;
$attachment[$file] = $title;
} else {
//this is a http type document, so just link straight to it.
$message .= '<br />' . '<a href=' . $file . '>' . $title . '</a>';
}
if (count($mails) > 0) {
$results[] = \App\Mailer::sendFromTemplate([
'template' => 'ZcoSendPDFFile',
'moduleName' => 'Documents',
'recordId' => $recordId,
'to' => $mails,
'from' => $from,
'message' => $message,
'attachments' => $attachment,
//'smtp_id' => 2,
]);
}
$response = new Vtiger_Response();
$response->setResult($results);
$response->emit();
}
}
这是构建参数的js文件
$(document).ready(function () {
var form = document.getElementById("emailDocument");
form.onsubmit = function (event) {
event.preventDefault();
let thisInstance = this;
let fromEmail = $("#fromEmail").html();
let toEmail = $("#toEmail").val();
let message = $("#message").val();
let recordId = getParameterByName("record");
let attachment = $("#pdfAttach").html();
let params = {
'module': 'Documents',
'action': 'ZcoEmailFile',
'mode': 'process',
'from': fromEmail,
'to': toEmail,
'message': message,
'record': recordId,
'attachments': attachment
};
$.post({
url: "index.php",
data: params,
ContentType: "text/json",
success: function() {
alert('Email has been sent!');
},
beforeSend: function(xhr) {
$("#submitEmail").attr("disabled", "disabled");
},
complete: function() {
$("#submitEmail").removeAttr("disabled");
},
error: function(msg) {
alert(JSON.stringify(msg));
}
});
};
});
还有表格
<form id="emailDocument">
<div id="pdfAttach" value="{$FILENAME}" style=display:none;>{$FILENAME}</div>
<div class="row pdfForm">
<div class="col-md-5">
<div class="toFrom">From:</div>
<div><span id="fromEmail" class="ml-1">{$USER_MODEL->get('email1')}</span></div>
<br />
<div class="toFrom">To:</div>
<div><input type="text" id="toEmail" value="{$CONTACT->get('email')}"></div>
<br />
</div>
<div class="col-md-5" rowspan="2">
Additional Message:
<textarea type="text" id="message" rows="3" cols="50"></textarea></div>
<div class="col-md-2">
<br />
<input type="submit" value="Submit" id="submitEmail">
</div>
</div>
</form>
电子邮件发送正常,包含除附件之外的所有字段。我已经六年没有使用 PHP 了,所以可以说我有点生疏了。谁能看出为什么附件没有附加吗?
在@Synchro 的帮助下,我解决了我的问题。我的 PHP 文件未正确调用附件。该字段是一个数组,我试图将其变成一个字符串。一旦我将 $attachment 设置为等于 PHPMailer 中内置数组中的值,它就可以毫无问题地发送。
添加代码:
$attachment = array($path, $file, $name, $encoding, $type, false, $disposition, $name);