Yeti CRM 不会使用 PHPMailer 附加文​​件

Yeti CRM will not attach files using PHPMailer

我在 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);