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