0

I'm trying to modify an existing PHP email function on the Yeti CRM platform. Right now the function sendFromTemplate($params) takes the collected form variables(stored in $params) and sends it to another function that writes it to a database table to be queued and sent every five minutes. I am trying to use PHPMailer or native PHP to send the email directly but I keep getting errors.

In the code I commented out the addMail function that does the databse queuing and instead set $params = $mailer->send(). I get the error: " PHP Fatal error: Uncaught Error: Call to a member function send() on null.

Other code I've tried and the error

$params = $mailer->send($params); PHP Fatal error: Uncaught Error: Call to a member function send() on null

$params->mailer->send(); PHP Fatal error: Uncaught Error: Call to a member function send() on null

return ['result' => $params->send(), 'error' => implode(PHP_EOL, $params->error)]; PHP Fatal error: Uncaught Error: Call to a member function send() on array

Here is the function that I am modifying.

public static function sendFromTemplate($params)
    {
        Log::warning('Send mail from template', 'Mailer');

        //error_log('Testing - sendFromTemplate');

        if (empty($params['template'])) {
            Log::warning('No templete', 'Mailer');

            return false;
        }

        $recordModel = false;
        if (empty($params['recordModel'])) {
            $moduleName = $params['moduleName'] ?? null;
            if (isset($params['recordId'])) {
                $recordModel = \Vtiger_Record_Model::getInstanceById($params['recordId'], $moduleName);
            }
        } else {
            $recordModel = $params['recordModel'];
        }
        //error_log('Template Name: ' . $params['template']);
        $template = Mail::getTemplete($params['template']);
        //error_log('Testing - template: ' . print_r($template, false));
        if (!$template) {
            Log::warning('No mail templete', 'Mailer');

            return false;
        }

        //error_log('have a template and all');
        $textParser = $recordModel ? TextParser::getInstanceByModel($recordModel) : TextParser::getInstance($params['moduleName'] ?? '');
        if (!empty($params['language'])) {
            $textParser->setLanguage($params['language']);
        }
        if (!empty($params['sourceRecord'])) {
            $textParser->setSourceRecord($params['sourceRecord'], $params['sourceModule']);
        }
        $textParser->setParams(array_diff_key($params, array_flip(['subject', 'content', 'attachments', 'recordModel'])));
        $params['subject'] = $textParser->setContent($template['subject'])->parse()->getContent();
        $params['content'] = $textParser->setContent($template['content'])->parse()->getContent();

        //Added to allow for something extra from customer service.
        $params['content'] = str_replace("$(zcoMessage)", $params['message'], $params['content']);

        unset($textParser);
        if (empty($params['smtp_id']) && isset($template['smtp_id'])) {
            $params['smtp_id'] = $template['smtp_id'];
        }
        if (isset($template['attachments'])) {
            $params['attachments'] = array_merge(empty($params['attachments']) ? [] : $params['attachments'], $template['attachments']);
        }
        if (!empty($template['email_template_priority'])) {
            $params['priority'] = $template['email_template_priority'];
        }
        $params = $mailer->send();
        return true;
        //return static::addMail(array_intersect_key($params, array_flip(static::$quoteColumn)));
    }

I have no idea what I am doing wrong at this point so any input would be appreciated. I just need to send an email.

  • `$mailer` is not in scope.. presuming you have set it somewhere? – Lawrence Cherone Jun 14 '19 at 20:50
  • @LawrenceCherone This CRM is very intricate so it may be set in another location but I'm going to go with it is not. For a minute let's pretend that I'm an idiot (not that hard). Where/How is the best way to set $mailer in this example? – BatmanPrime Jun 14 '19 at 21:12
  • I've no idea what this is doing, but I can tell that `$mailer` is not a PHPMailer instance because PHPMailer's `send()` method does not return an array, which this code is clearly expecting. From what I can see here, no sending params are being set at all (from, to, server, id, password, etc), it's *only* about the content, so I think you need to look elsewhere to see where `$mailer` gets defined.. – Synchro Jun 14 '19 at 21:36

0 Answers0