简体   繁体   中英

Yeti CRM will not attach files using PHPMailer

I am using a form in the Yeti CRM that allows the user to view a document then gives them the option to email said document. It is using PHPMailer but I cannot get it to attach the document. All other fields are responding to edits (To, From, Subject, Body) but nothing is attaching. Error logs do not throw an error.

I have tried directly linking a file that I know exists but still no luck. Can anyone see in the code below where I am not actually attaching the document?

Here is the PHP function

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();
}

}

This is the js file that builds the params

$(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));
        }
    });
};

});

And the form

<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>

The email sends fine with all of the fields except the attachment. I haven't used PHP in six years so suffice to say I'm a little rusty. Can anyone see why the attachment is not attaching?

With a little help from @Synchro I have solved my issue. My PHP file was not calling the attachment correctly. The field is an array and I was trying to make it a string. Once I set $attachment equal to the values in the array built in PHPMailer it sent without issue.

Code added: $attachment = array($path, $file, $name, $encoding, $type, false, $disposition, $name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM