How to send message programmatically

Created on 20 May 2024, 6 months ago
Updated 3 June 2024, 6 months ago

Create new message and new threads (the most used)

/** @var \Drupal\privatemsg\PrivateMsgServiceInterface $service */
$service = \Drupal::service('privatemsg.common');
// User ids.
$members = [1, 2];
$service->createNewMessageAndThreads(1, 'test message', 'full_html', 'create message programmatically', $members);

Create new message and attach to existing thread by properties

/** @var \Drupal\privatemsg\PrivateMsgServiceInterface $service */
$service = \Drupal::service('privatemsg.common');
$message = $service->createNewMessage(1, 'test message', 'full_html');
$properties = [
  'subject' => 'create message programmatically 2',
];
$service->attachMessageToExistingThreadsByProperties($properties, $message->id());

You can also attach message to threads by threads ids

/** @var \Drupal\privatemsg\PrivateMsgServiceInterface $service */
$service = \Drupal::service('privatemsg.common');
$message = $service->createNewMessage(1, 'test message', 'full_html');
// User ids.
$members = [1, 2];
$ids = $service->createNewThreads('create message programmatically 3', $members);
$service->attachMessageToExistingThreadsByIds($ids, $message->id());
📌 Task
Status

Fixed

Version

2.0

Component

Documentation

Created by

ivnish Kazakhstan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024