function privatemsg_reply

1 privatemsg.module privatemsg_reply($thread_id, $body, $options = array())
7-1 privatemsg.module privatemsg_reply($thread_id, $body, $options = array())
7-2 privatemsg.module privatemsg_reply($thread_id, $body, $options = array())
6-2 privatemsg.module privatemsg_reply($thread_id, $body, $options = array())

Send a reply message

This functions replies on an existing thread.


$thread_id: Thread id

$body: The body text of the new message

$options: Additional options, possible keys: author => User object of the author timestamp => Time when the message was sent

Return value

An array with a key success and messages. This key contains an array where the key is the error type (error, warning, notice) and an array with messages of that type.. If success is TRUE, it also contains a key $message with the created $message array, the same that is passed to hook_privatemsg_message_insert().

It is theoretically possible for success to be TRUE and message to be FALSE. For example if one of the privatemsg database tables become corrupted. When testing for success of message being sent it is always best to see if ['message'] is not FALSE as well as ['success'] is TRUE.

Example messages values:

  array('error' => array('A error message'))

Related topics


./privatemsg.module, line 1725
Allows users to send private messages to other users.


function privatemsg_reply($thread_id, $body, $options = array()) {
  global $user;
  $author = clone $user;

  $message = (object) $options;
  $message->body = $body;

  // Apply defaults - this will not overwrite existing keys.
  if (!isset($message->author)) {
    $message->author = $author;
  if (!isset($message->timestamp)) {
    $message->timestamp = REQUEST_TIME;
  if (!isset($message->format)) {
    $message->format = filter_default_format($message->author);

  // We don't know the subject and the recipients, so we need to load them..
  // thread_id == mid on the first message of the thread
  $first_message = privatemsg_message_load($thread_id, $message->author);
  if (!$first_message) {
    return array(
      'success' => FALSE,
      'messages' => array('error' => array(t('Thread %thread_id not found, unable to answer', array('%thread_id' => $thread_id)))),

  // Add the reply_to_mid, so we can mark that message as replied
  $thread = privatemsg_thread_load($thread_id);
  $message->reply_to_mid = end($thread['messages'])->mid;

  $message->thread_id = $thread_id;
  // Load participants
  $message->recipients = _privatemsg_load_thread_participants($thread_id, $message->author);
  $message->subject = $first_message->subject;
  $validated = _privatemsg_validate_message($message);
  if ($validated['success']) {
    $validated['message'] = _privatemsg_send($message);
    if ($validated['message'] !== FALSE) {
      _privatemsg_handle_recipients($validated['message']->mid, $validated['message']->recipients, FALSE);

  return $validated;