After some hours trying to find out why notifications with the "Private Message" sending method active never arrive to the destination user, I discovered that there's no way to send private messages if not logged in as a user. Cron is always called as anonymous user, I think It wouldn't be a good practice to do it from a logged user.

It has no sense to send notifications with a real sender, the sender inbox will increase up to thousands of messages....
I don't know if there's anyone that has been able to send private notifications from cron, perhaps using a "fake" sender or any other workaround.
Maybe I am missing something.

Thanks for your help.

Comments

aleada’s picture

Hi I am not sure but I thing that i have the same problem.
Did you find any solution?

aleada’s picture

I found somewhere that there is an option to run cron as a specific user. I will find that and I will post the solution ;)

aleada’s picture

http://drupal.org/cron gives some tips on cron and how to run cron as authenticated user.

But the pm - and the notifications must created even when the cron run as anonymous.....
This is a bug .

I am trying to run the cron as authenicated using the script with no results....

dave reid’s picture

Drupal 7 will automatically switch to the anonymous user when runnign cron, so this is a bug that needs to be fixed in this module.

jose reyero’s picture

Project: Messaging » Privatemsg
Version: 6.x-2.2 »

I don't think switching cron user is the solution here, as Messaging can send multiple messages as different users during a single cron run.

Moreover, messages can be sent as coming from the system (anonymous user) depending on Notificaitons options.

So I'd ask from Privatemsg that it did the permission checking at a 'higher' level (form submission?) and we can have a permission-free API to just send messages.

berdir’s picture

Well, there is an internal function which *can* be used, but I'm pretty sure you don't want that.

Right now, Privatemsg does not support an anonymous author, you always need to specify one. (this does not need to be the currently logged in user, see http://blog.worldempire.ch/api/function/privatemsg_new_thread/1 ($options => array('author' => $some_user). Privatemsg *will* check permissions for the specified author, not for the currently logged in user.

In #490264: New sub-module: Site-wide contact form, which allows anonymous users to send contact messages which are handled by Privatemsg, we have configuration option to configure the author. By adding that user to a specific role and adding a configuration, you can also disallow users to write/reply to that system user.

aleada’s picture

Ok,
so the best way to bypass the problem is by selecting on the notification settings to use the system (anonymous user) as sender and not the user.
The cron will process and send private messages to all subscribers using as sender the "site".

Please correct me if I am wrong.

jose reyero’s picture

> Privatemsg does not support an anonymous author

I'm afraid that makes it not suitable as a Messaging plug-in. There are times when the 'sender' may be the system itself or anonymous.

Is there a reason for such restriction? Will it break if I create a row with 'author' = 0?

I'm thinking of a workaround that is setting up the user receiving it as the sender too (which may be ok as you can look at it as a message sent to yourself because you subscribed to something).

Would that work?

However I'd prefer if we have a clean API without enforcing permissions (so they are checked previously). Btw, is there really a reason to need that global user switching?

aleada’s picture

Thats exactly my point.
The private messaging is not suitable as send method for notifications ( subscribe to contents ). Your workaround looks enough!
It is very logical, when someone wants to receive content notifications, to choose private messages and the sender - receiver could be the same. I choose to get notifications so I send a message in my inbox (site inbox) for every notification event triggered by the system.

One problem should be that the private messaging does not support receipient same with the sender.... Any idea?

I have also the following idea.
Lets assume that the user choose the private messaging as send method for notifications and the only available send interval is the 0 - immediate. Also the user has the email notification for private messages enabled .
This settings will cause the immediate sending of many email messages (same number as the number of subscribers to the content type notification ) . This is not good for the server performance, because it will produce a big number of notify emails for every private message created.

There is a module called job queue (http://drupal.org/project/job_queue). Is it possible to send the notify email for private messages inside the queue of the job queue? If yes, the total number of email messages will be send in a few cron runs, so the server load will be normal at any time without spikes.

Another idea is to keep the private messaging only for messages accross users (pm). Is any way to unregister (hide) the private messaging from the send methods of the notifications module? By disabling the private messages as send method the users could be informed for notification events using the other methods ( mail, sms etc) but also they can use private messaging - email notification for pm , only for messages exchanged with other users excluding the system events ( node creation, updates, comments etc).

berdir’s picture

I'm afraid that makes it not suitable as a Messaging plug-in. There are times when the 'sender' may be the system itself or anonymous.

Is there a reason for such restriction? Will it break if I create a row with 'author' = 0?

The reason is simply that Privatemsg is designed as a user to user messaging system. No, it will not break when you do that, but you will recieve a message from "Anonymous User". Maybe we could handle these specially and simply say that they are sent by "System" or so. Feel free to open a feature request about that.

Until then, you can either use the recipient as author too as you proposed or let users define an account to be used as author, similiar to what we do in the linked privatemsg_contact module.

One problem should be that the private messaging does not support receipient same with the sender.... Any idea?

We do support that.

Also, sending notification mails doesn't have anything to do with this issue. If that's a problem for you, there are modules which allow to automatically add messages to a queue instead of sending them directly, for example http://drupal.org/project/queue_mail

Btw, is there really a reason to need that global user switching?

Where do you need to do that? We need to do it internally for some checks, but you can simply do the following:

privatemsg_new_thread(array($recipient), 'subject', 'message', array('author' => $recipient));
aleada’s picture

Ok Berdir I am little confused.

My case is the following.

I have a web site that all the users has the right to create nodes (cck, blogs etc) and also create comments.
I also need the private messaging to give to my users a web based inbox but in the same time i want my users to be notified whenever a new message took place on their inbox. The private messaging is very nice because the original email of the user is not visible to other users but the user is informed for messages arrived on his site-based inbox.

1) My settings on private messaging (admin/settings/messages/notify) is to " Notify users of new private messages by default " = checked .
Every user has the right to change this setting on his user account edit page.

I also want to use the private messaging as send method for site notifications. Here is the problem.

The available intervals are Immediate, hourly, daily, weekly, monthly etc.

2) My default interval is Immediatly.
Every time that a content created-updated-commented the users getting a notification - private message inside their inbox. The sender of the message is always the author of the content-comment.
The first problem is that the user who created the node has on his private messaging sent box a total number of messages equal to the number of the subscribers to the specific content type. That is not very bad, but is not very nice for the user because he feels that any move will cause a huge number of messages with his account as the sender.....
The second problem is that the combination of the 1 and the 2 create a huge load to the server because the notify for private message emails are to many and they processed at the same time with the node creation - update comment etc. The node takes long time to be saved because in the background the server is trying to deliver to the total number of the subscribers a new notification mail containing the message that the user has a new private message on the site (which contains the actual notification).

I tried to change the default interval for the notifications to hourly. This could be more eficient because the notify messages for pm will be processed at later time by the cron and the server load will be balanced. This setting caused no private messages at all . None of the subscribed users took any message and no email notification for pm sent by the server to anyone.

So now I am confused and I don't know what to do to solve the problem. Notifications are crucial for my web site , private messaging also. The usage of private messaging as send method for notifications works only for interval 0 (Immediate) and does not work for any other interval value.
What can I do to solve the problem?
Could you please give me a path to follow? I am not a drupal expert :( and I feel lost in space ;)

Thanks in advance!

berdir’s picture

Project: Privatemsg » Messaging
Version: » 6.x-2.x-dev
Category: support » bug

The usage of private messaging as send method for notifications works only for interval 0 (Immediate) and does not work for any other interval value.

Yes, this is a bug, and we are discussing how to fix this. There are multiple possible solutions, as explained above.

1. Use the recipient is author: This is a single line change for Messaging.module and can be done without touching privatemsg.module at all. So it could also be a workaround until an other solution is implemented. As explained in #10, it is *not* necessary to switch cron user or anything like that, our API supports this use case.

2. Allow to define a author account, which is then used for all private messages sent with messaging.module (unless otherwise specified maybe?). Also, this can be done in messaging.module only and doesn't require changes to Privatemsg.

3. Add a feature to Privatemsg which allows to send messages as "the System", without needing an actual user account. This requires changes in Privatemsg and a separate feature request.

@Jose: Giving this back to you, if you want to implement #3 then open a separate feature request please. But until that would be implemented, you can still fix the actual bug with either 1 or 2.

Edit:

The second problem is that the combination of the 1 and the 2 create a huge load to the server because the notify for private message emails are to many and they processed at the same time with the node creation - update comment etc. The node takes long time to be saved because in the background the server is trying to deliver to the total number of the subscribers a new notification mail containing the message that the user has a new private message on the site (which contains the actual notification).

Yes. This is however a different issue and should not be handled here. As explained above, there are solutions which allow to handle *all* mails with a queue. I do not plan to extend our mail notify module with job queue support.

jose reyero’s picture

Status: Active » Fixed

@Berdir,

I'd prefer option #3 so please reopen the issue if we ever have that. Also checking permissions or using global user inside an API makes it quite unsuitable for some things, like cron processing.

In the meanwhile we need a solution so after looking into privatemsg, I think our best option is to bypass validation and use directly _privatemsg_send().

Updated messaging_privatemsg, done some quick testing (not cron), seems to work.

Thanks.

jose reyero’s picture

I forgot to add I've done that in the 4.x branch. If anyone can confirm it works, we can backport it to 2.x

aleada’s picture

well my conclusion is :

Using the 1 the user will notify "his self" and he will be appeared as sender of the notifications. This can be used with different send intervals for notifications. Am I right?

Using the 2 a specified user "notify_user" for example will be placed as sender for every notification sent . That is not very nice because many users will try to reply to this user although he is not a real user but a batch job user. Also this user will be placed as sender even when the users send pm one to another.

Using 3 is very clear but is not implemented for drupal 6. How can we help you Jose to port your solution in D6 ???

The most easy solution for me is the 1 .
I tried to find where is the right place to put the user as sender on Messaging.module, but I terrified. There are many many methods,
with many arguments and I don't know how to do the change. I was waiting to find an array of subscribers (users) and inside a loop to put something like $sender = $user .....

Could anybody give me a hint?
Thanks!

jose reyero’s picture

@amatetch,

I was talking about D6, see the changes in 6.x-4.x-dev, that are the ones to backport to 6.x-2.x if someone can confirm it works (cron, etc..)

aleada’s picture

@Jose
Ok sorry,
I will test it tonight on my dvlp setup!
Where is the 4 version ? The only available dev is privatemsg 6.x-2.x-dev

jose reyero’s picture

@amatech,

I mean Messaging & Notifications 4.x

aleada’s picture

1. Use the recipient is author: This is a single line change for Messaging.module and can be done without touching privatemsg.module at all. So it could also be a workaround until an other solution is implemented. As explained in #10, it is *not* necessary to switch cron user or anything like that, our API supports this use case.

How can I make this modification ? Any code hint - snippet?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Michsk’s picture

I also cant figure out how to set the sender for the privatemsg messaging option. Personally i would love a option like we got for mail /admin/messaging/settings/method/mail. That would work nice for privatmsg aswell. Offcourse with the use of Token.

Michsk’s picture

Status: Closed (fixed) » Active
Michsk’s picture

I addition to this. Privatemsg also has the option 'only receive messages from users'. And because the privatemsg messaging option send as an anonymous user. Users wont even get the privatemsg.

Seighaus’s picture

For the record (It took me a while to find this, so maybe it helps others): On the Messaging modules sites it says: "Privatemsg, integrating with Privatemsg module (For Drupal 6 it needs the dev version of privatemsg)"
edit: sorry, updating to newest dev didn't solve the problem.

christianchristensen’s picture

StatusFileSize
new1.96 KB

Re: #10

Given this specific example - here is a quick patch that accomplishes this goal in the PM messaging plugin processor.

christianchristensen’s picture

StatusFileSize
new1.27 KB

oops - here's a better patch.

marinex’s picture

@lasac
I looked at the privatemsg and I cannot found where is the options 'only receive messages from users'. Can you help me where is the option?

marinex’s picture

StatusFileSize
new2.84 KB

Hi all :-) I recreated messaging_privatemsg modul so now you can send notification over privatemsg upon run cron :-) lets go test it and commnet :-) This module is for 4.x D6

marinex’s picture

StatusFileSize
new2.84 KB

:-) small update in the install file. This module is for 4.x D6

marinex’s picture

StatusFileSize
new3.14 KB

Add readme.txt with informations about settings page... This module is for 4.x D6