Prefill the Subject of a Privatemsg Message

I've had a nice go at making private messaging capabilities for flockNote work a lot nicer than the out-of-the-box Privatemsg module experience, by simplifying everything to the point that it's closer to the Facebook Direct Message system than the normal Privatemsg UX. (Privatemsg is the premiere way of handling private messaging in Drupal. It's already awesome out of the box... just needed a bit more help for our particular site ;-).

One thing I had wanted to do for a while is prefill the subject field of certain messages. I already have the new private message page appear inside an overlay popup after a user clicks on a link to send a private message to another user on the site.

Privatemsg prefill subject

I wanted users sending direct messages regarding certain comments or nodes to have a subject line of 'RE: [node title]' or 'RE: [comment title]' in them so they didn't have to write out a subject on their own. I was prepared to implement a hook_form_alter for the privatemsg form, send a query fragment containing the private message subject in the URL to the new private message page, and then check for it in my form alter to fill it in as the subject... but it turns out the Privatemsg module already has this capability built in!

All you need to do is throw in the subject as an extra argument in the url like so: messages/new/[uid]/[subject]. (You can still throw on query fragments to the end if you need to. In my case, I put a destination on the end so the overlay closes after a message is sent).

Or, in code...

= user_load($node->uid);
$pm_link_text = t('Send a PM to Author');
$pm_url = privatemsg_get_link($account) . '/' . t('RE: @title', array('@title' => $node->title));
$pm_link = l($pm_link_text, $pm_url, array('query' => array(drupal_get_destination())));


Speaking of out of the box awesomeness, Privatemsg 6.x-2.x and 7.x actually provides this already for nodes and comments, you just need to enable for which content types you want.

Yes, including a pre-filled subject based on the node/comment title, although not exactly what you're using, but you could override that using string overrides. Or we could make what you're using the default, I actually like that more.. (Patches are welcome). See… and…

I noticed that in a comment on an issue somewhere on but for my needs, I have to build the link manually since I'm inserting it above the comment reply field (see:

Maybe instead of defaulting to 'RE: [title]', you could allow tokens to be used and allow the setting of the subject in the interface... But I think it works great as-is for now.

Jeff, this is a great post, and your site and service look really terrific. You obviously are very competent at the privatemsg-altering stuff, and I was hoping you can suggest a solution to a couple of problems I have. I've tried posting a support issue on the project, and an issue on stackoverflow - but no responses yet. Also, I tried a using the form_privatemsg_new_alterfield hook to disable autocomplete as the maintainer had previously suggested, but it did not work. Thanks for your post, and any guidance you can provide. Here are my two issues:
When a user accesses another user's profile, the link "Send this user a private message" is displayed. When clicked, the message-creation form is displayed. I was hoping the recipient's address on this form would be automatically populated and would be read-only. But the bigger problem is that with the autocomplete address field, the sender has access to all user names (which, in my app, is an unacceptable security breach). I am using the latest dev version of the module.

Hey Jeff,

Any idea how I'd be able to hook into the email that's sent to notify the recipient? I'm using profile2 and I'd like to use a field from the profile vice the username in the message details.

Hello, the site you worked on looks great. I am trying to reach half of your skills here, and the first issue i run into is that i don't know how to -instead of calling a user- call a role from the url, that is: messages/new/[rid]/[subject]

What would be an example of a rid? where can i find it, and what's most important, can you do that?



The rid is a 'role id', which wouldn't be very helpful in sending private messages, because PMs can only be sent from one user to another. An rid might be helpful if there were a way to send a message to all members of a particular drupal role (permission set), but that would need to be added functionality, as it's not included in the privatemsg module.


Can you tell me how to show private message page in lightbox. Also can you tell me where to put your php code, in a module or view

I put the code in a custom module, and we're simply using the overlay module, built into Drupal 7 core. To add the private message path, you just need to implement hook_admin_paths().

Thanks, it worked. But I used ctools api to display the form in lightbox. It is really interesting.