We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36571
    • 145 Posts
    A slightly strange problem has turned up.

    When trying to send a message to a user from their profile page, I get the error message saying "Could not instantiate mail function". However, when I remove the &viewProfileTpl parameter, the mail gets sent. However, when it arrives, it has lost both it’s subject line and body.

    I don’t really trust myself to start messing around with the php mailer class, and I am sure that there is a fix for this.

    Any help would be much appreciated.
      • 24731 ☆ A M B ☆
      • 211 Posts
      I have exactly the same problem, have not yet figured out what’s wrong. Where you able to sort it out?

        • 24731 ☆ A M B ☆
        • 211 Posts
        okay, I am trying to debug, I already found something wrong. The webloginpe class has this around lines 1543-1548:

        		if (empty($subject) || $subject = '' || empty($message) || $message = '')
        		{
        			$this->FormatMessage($this->LanguageArray[0]);
        			$this->ViewUserProfile($you['username']);
        			return;
        		}


        I’m guessing it should be:

        		if (empty($subject) || $subject == '' || empty($message) || $message == '')
        		{
        			$this->FormatMessage($this->LanguageArray[0]);
        			$this->ViewUserProfile($you['username']);
        			return;
        		}


        I keep looking and post back.
          • 24731 ☆ A M B ☆
          • 211 Posts
          Okay, I figured it out, the problems I found where:

          1st:

          empty($subject) || $subject = ’’ || empty($message) || $message = ’’

          When it should have been:

          empty($subject) || $subject == ’’ || empty($message) || $message == ’’

          That may explain why the form was arriving empty for the user above.

          The second problem was this:

          $me = $modx->getWebUserInfo($modx->db->escape($_POST[’me’]));
          $you = $modx->getWebUserInfo($modx->db->escape($_POST[’you’]));

          These you and me variables are not present in POST. I did two things, first was, to send the user to send the msg to as a hidden input in the form. The second was, instead of sending the hidden field for the user sending the form, I decided to pick it up from the session instead since that page is only viewable to members when they are login anyways.

          It might be a smarter idea to leave the default values (for future upgrades) and simply send the me hidden form input field instead, I just didn’t do it, but it should work as well.

          here is the code:

          	function SendMessageToUser()
          	{
          		global $modx;
          		
          		$me = $modx->getWebUserInfo($modx->db->escape($_SESSION['webInternalKey']));
          		$you = $modx->getWebUserInfo($modx->db->escape($_POST['you']));
          		$subject = $modx->db->escape($_POST['subject']);
          		$message = stripslashes(strip_tags($_POST['message']))."\n\n".$modx->config['site_name'];
          
          		if (empty($subject) || $subject == '' || empty($message) || $message == '')
          		{
          			$this->FormatMessage($this->LanguageArray[0]);
          			$this->ViewUserProfile($you['username']);
          			return;
          		}
          
          		$EmailMessage = new PHPMailer();
          		$EmailMessage->From = $me['email'];
          		$EmailMessage->FromName = $me['fullname']." (".$me['username'].")";
          		$EmailMessage->Subject = $subject;
          		$EmailMessage->Body = $message;
          		$EmailMessage->AddAddress($you['email'], $you['fullname']);
          
          		if (!$EmailMessage->Send())
          		{
          			$this->FormatMessage($EmailMessage->ErrorInfo);
          			$this->ViewUserProfile($you['username']);
          			return;
          		}
          		$this->FormatMessage($this->LanguageArray[37].' "'.$you['username'].'"');
          		$this->ViewUserProfile($you['username']);
          		return;
          	}


          or

          	function SendMessageToUser()
          	{
          		global $modx;
          		
          		$me = $modx->getWebUserInfo($modx->db->escape($_POST['me']));
          		$you = $modx->getWebUserInfo($modx->db->escape($_POST['you']));
          		$subject = $modx->db->escape($_POST['subject']);
          		$message = stripslashes(strip_tags($_POST['message']))."\n\n".$modx->config['site_name'];
          
          		if (empty($subject) || $subject == '' || empty($message) || $message == '')
          		{
          			$this->FormatMessage($this->LanguageArray[0]);
          			$this->ViewUserProfile($you['username']);
          			return;
          		}
          
          		$EmailMessage = new PHPMailer();
          		$EmailMessage->From = $me['email'];
          		$EmailMessage->FromName = $me['fullname']." (".$me['username'].")";
          		$EmailMessage->Subject = $subject;
          		$EmailMessage->Body = $message;
          		$EmailMessage->AddAddress($you['email'], $you['fullname']);
          
          		if (!$EmailMessage->Send())
          		{
          			$this->FormatMessage($EmailMessage->ErrorInfo);
          			$this->ViewUserProfile($you['username']);
          			return;
          		}
          		$this->FormatMessage($this->LanguageArray[37].' "'.$you['username'].'"');
          		$this->ViewUserProfile($you['username']);
          		return;
          	}


          You also need to post from your form the userid to send the mail to, like this:

          <input type="hidden" name="you" value="[+view.id+]" />


          and if sending the me in the post as well:

          <input type="hidden" name="you" value="use function to get your sender user id, maybe phx?" />


          Regards,
          Jose R. Lopez