Why is getPreparedMailHandler Returning False?

TheBigK

Well-known member
I'm trying to create my own 'Contact Us' page, and obviously; most of my code matches with that of the existing code. I'm however unable to get it to run fine.

Debugging shows that 'getPreparedMailHandler' is returning 'false' and I've no clue what's there that's failing. The actual error I'm getting is this:-

Fatal error: Call to a member function setReplyTo() on boolean in /Applications/MAMP/htdocs/xf/library/ContactUs/ControllerPublic/Index.php on line 69

The line 69 is: $mailObj ->setReplyTo($replyTo, $replyName);


PHP:
  //Build arguments for $mailObj
            $toEmail = 'me@mydomain.com'; //This will later be updated to support department email IDs
            $toName = 'MyWebsite';
            $headers = array('Sender' => 'me@mydomain.com');
            $fromMail = 'me@mydomain.com';
            $fromName = 'MyWebsite';

            $mailObj = $mail->getPreparedMailHandler($toEmail, $toName, $headers, $fromMail, $fromName);
            $replyTo = $contactEmail;
            $replyName = $contactName;
            $mailObj->setReplyTo($replyTo, $replyName);
            $mail->sendMail($mailObj);

            return $this->responseRedirect(
                XenForo_ControllerResponse_Redirect::SUCCESS,
                $this->getDynamicRedirect(),
                new XenForo_Phrase('your_message_has_been_sent')
            );
 
I think I need to offer the complete code of my controller -

PHP:
class ContactUs_ControllerPublic_Index extends XenForo_ControllerPublic_Abstract
{
    // Show form fields and populate 'Name Field' with Username
    public function actionIndex()
    {
        // Get the Contact Model
        $contactModel = $this->_getContactModel();

        // Get user information, and derive username and email
        $user = XenForo_Visitor::getInstance()->toArray();
        $username = $user['username'];
        $useremail = $user['email'];

        // Build View Parameters
        $viewParams = array(
            'username'  => $username,
            'useremail' => $useremail
        );

        return $this->responseView(
            'Contactus_ViewPublic_Index', "ce_contactus", $viewParams
        );
    }

    // Submit all the information to mail queue and also create a log in the backend

    public function actionSend()
    {
        // Work for POST request only
        if($this->_request->isPost())
        {
            $user = XenForo_Visitor::getInstance()->toArray();
            // Collect all the parameters from the form in filtered form
            $contactName = $this->_input->filterSingle('contact_name', XenForo_Input::STRING);
            $contactEmail = $this->_input->filterSingle('contact_email', XenForo_Input::STRING);
            $contactPhone = $this->_input->filterSingle('contact_phone', XenForo_Input::INT);
            $department = $this->_input->filterSingle('contact_department', XenForo_Input::STRING);
            $message = $this->_input->filterSingle('contact_message', XenForo_Input::STRING);




            //No Flooding
            $this->assertNotFlooding('Index');

            // Now compile all the mail paramters

            $mailParams = array(
                'user' => $user,
                'subject' => $department,
                'message' => $message,
            );

            //Mail Writer
            $mail = XenForo_Mail::create($department, $mailParams, 0);

           
  //Build arguments for $mailObj
            $toEmail = 'me@mydomain.com'; //This will later be updated to support department email IDs
            $toName = 'MyWebsite';
            $headers = array('Sender' => 'me@mydomain.com');
            $fromMail = 'me@mydomain.com';
            $fromName = 'MyWebsite';

            $mailObj = $mail->getPreparedMailHandler($toEmail, $toName, $headers, $fromMail, $fromName);
            $replyTo = $contactEmail;
            $replyName = $contactName;
            $mailObj->setReplyTo($replyTo, $replyName);
            $mail->sendMail($mailObj);

            return $this->responseRedirect(
                XenForo_ControllerResponse_Redirect::SUCCESS,
                $this->getDynamicRedirect(),
                new XenForo_Phrase('your_message_has_been_sent')
            );
        }

        else
        {
            $viewParams = array(
                'redirect' => $this->getDynamicRedirect(),
                'isOverlay' => $this->_noRedirect() ? true : false,
            );

            return $this->responseView('ContactUs_ViewPublic_Misc_Contact', 'ce_contactus', $viewParams);
        }

    }



    protected function _getContactModel()
    {
        return $this->getModelFromCache('ContactUs_Model_Contact');
    }
}
 
The error seems to be in the template "ce_contactus".
Check which values for 'contact_name' and 'contact_email' are transferred.
 
The error seems to be in the template "ce_contactus".
Check which values for 'contact_name' and 'contact_email' are transferred.
HTML:
<xen:title> Contact CrazyEngineers Team </xen:title>
<xen:description> Please provide appropriate information so that we can assist you </xen:description>
<xen:comment> FIGURE OUT HOW TO BUILD BREADCRUMBS TO SHOW HOME > CONTACT US
<xen:navigation>
    <xen:breadcrumb source="$nodeBreadCrumbs" />
</xen:navigation>
</xen:comment>


<form action="{xen:link 'contactus/send'}" method="post" id="ContactUsForm" data-redirect="on" >
<fieldset>
    <dl class="ctrlUnit fullWidth surplusLabel">
        <dt><label for="ctrl_contact_name"> Your Full Name Or Username : <br/></label></dt>
        <dd><input type="text" name="contact_name" class="textCtrl titleCtrl" id="ctrl_contact_name" size="40" autofocus="true" value={$username} />
       
       
        <dt><label for="ctrl_contact_email"> Your Email Address : </label></dt>
        <dd><input type="text" name="contact_email" class="textCtrl titleCtrl" id="ctrl_contact_email" size="40" value={$useremail} />
        <p class="explain">Please ensure that the above email address is correct </p>
       
        <dt><label for="ctrl_contact_phone"> Your Contact Phone : </label></dt>
        <dd><input type="text" name="contact_phone" class="textCtrl titleCtrl" id="ctrl_contact_phone" size="40" />
       
        <dt><label for="ctrl_contact_department"> Select the department you'd like to contact :</label></dt>
        <dd><li>
            <select name="contact_department" class="textCtrl titleCtrl autoSize" id="ctrl_contact_department">
           
                <option value="department1"> Department 1 </option>
                <option value="department2"> Department 2 </option>
                <option value="department3"> Department 3 </option>
               
            </select>
           
        </li></dd>
       
        <dt><label for="ctrl_contact_message"> Please write the details :</label></dt>
        <dd><textarea name="contact_message" id="ctrl_contact_message" class="textCtrl titleCtrl" rows="7" cols="50"></textarea></dd>
       
       
        <input type="submit" value="Send" accesskey="s" class="button Primary" />
        <input type="hidden" name="_xfToken" value="{$visitor.csrf_token_page}" />
       

    </dl>
</fieldset>
</form>
 
Update:

Debug information shows that the variables in my controller are getting correct values from the template. So that can't be the issue.

Screen Shot 2015-08-17 at 3.23.48 pm.webp
 
Code:
$mail = XenForo_Mail::create($department, $mailParams, 0);
The name of your email template is the department? The mail will return false if you give it an invalid template name.
 
Code:
$mail = XenForo_Mail::create($department, $mailParams, 0);
The name of your email template is the department? The mail will return false if you give it an invalid template name.
Thanks a lot!

I read the 'create' definition :
public static function create($emailTitle, array $params, $languageId = null)

and didn't bother about checking $emailTitle is actually the email Title template. Maybe change it to $emailTemplateTitle in the next version? :D
 
Top Bottom