Include PM body in notification email

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
AlManahry
Registered User
Posts: 13
Joined: Tue Mar 12, 2019 5:44 pm

Include PM body in notification email

Post by AlManahry » Sat Mar 16, 2019 5:00 pm

Hello ,

Am trying to include the PM body in the notification email !

in an old post for a very old version it mentioned adding 'MESSAGE' line in functions_privmsgs.php , and editing privmsg_notify.txt to include {MESSAGE} , here's the link to that post : viewtopic.php?t=1046055

This doesn't work with 3.2.x apparently , functions_privmsgs.php seems to have this line already ! and by adding the {MESSAGE} to the notify text it puts a gap in the body but actual message body is not there .. tried {DECODED_MESSAGE} as well but no avail .

I appreciate if there's an answer to that :)

Thanks !

User avatar
kinerity
Community Team Member
Community Team Member
Posts: 2086
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: Include PM body in notification email

Post by kinerity » Sun Mar 17, 2019 12:01 am

Open: /phpbb/notification/type/pm.php

Find:

Code: Select all

			'SUBJECT'					=> htmlspecialchars_decode(censor_text($this->get_data('message_subject'))),
After, add:

Code: Select all

			'MESSAGE'					=> htmlspecialchars_decode(censor_text($this->get_data('message_text'))),
I haven't tested this, but it should work as long as you have {MESSAGE} in your privmsg_notify.txt e-mail template.
Kailey Truscott - Community Team

AlManahry
Registered User
Posts: 13
Joined: Tue Mar 12, 2019 5:44 pm

Re: Include PM body in notification email

Post by AlManahry » Sun Mar 17, 2019 1:22 am

Thanks a lot Kailey ,

the location is 100% correct , however I think the variable name is not message_text ! It doesn't work with message_text , I changed it to message_subject and it generated the subject twice ( one for {SUBJECT} and once for {MESSAGE} , I also tested message instead of message_text , didn't work ...

CarolC1
Registered User
Posts: 552
Joined: Sat Dec 02, 2006 4:26 pm

Re: Include PM body in notification email

Post by CarolC1 » Sun Mar 17, 2019 2:04 am


User avatar
mrgoldy
Jr. Extension Validator
Posts: 909
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Include PM body in notification email

Post by mrgoldy » Sun Mar 17, 2019 7:08 am

It will not work, cause a little lower in that file you can see what data is made available in the create_insert_array function. Which is only the user id and the subject.

So if you want the actual message to be available there aswell, you’ll have to find where the notification is triggered, include the message in the notification data, then make it available through the above mentioned function and then assign it as a template variable.
phpBB Studio / ''Proud member of the Studio"

AlManahry
Registered User
Posts: 13
Joined: Tue Mar 12, 2019 5:44 pm

Re: Include PM body in notification email

Post by AlManahry » Sun Mar 17, 2019 11:55 am

CarolC1 wrote:
Sun Mar 17, 2019 2:04 am
This may help

viewtopic.php?f=456&t=2460446
This works ,but it’s in Beta stage and the developer is not supporting it anymore and not moving it ro production so with any upgrade and change I will have to find anothwr way again :)

Maybe I can look in his code to see the missing lines in my code

AlManahry
Registered User
Posts: 13
Joined: Tue Mar 12, 2019 5:44 pm

Re: Include PM body in notification email

Post by AlManahry » Sun Mar 17, 2019 12:13 pm

mrgoldy wrote:
Sun Mar 17, 2019 7:08 am
It will not work, cause a little lower in that file you can see what data is made available in the create_insert_array function. Which is only the user id and the subject.

So if you want the actual message to be available there aswell, you’ll have to find where the notification is triggered, include the message in the notification data, then make it available through the above mentioned function and then assign it as a template variable.
thanks , that makes sense , I am "assuming" the notification is triggered from includes/functions_privmsgs.php .. MESSAGE seems included already in the template :

Code: Select all

               $template->assign_block_vars('history_row', array(
                        'MESSAGE_AUTHOR_QUOTE'          => (($decoded_message) ? addslashes(get_username_string('username', $author_id, $row['username'], $row['user_colour'], $row['username'])) : ''),
                        'MESSAGE_AUTHOR_FULL'           => get_username_string('full', $author_id, $row['username'], $row['user_colour'], $row['username']),
                        'MESSAGE_AUTHOR_COLOUR'         => get_username_string('colour', $author_id, $row['username'], $row['user_colour'], $row['username']),
                        'MESSAGE_AUTHOR'                        => get_username_string('username', $author_id, $row['username'], $row['user_colour'], $row['username']),
                        'U_MESSAGE_AUTHOR'                      => get_username_string('profile', $author_id, $row['username'], $row['user_colour'], $row['username']),

                        'SUBJECT'                       => $subject,
                        'SENT_DATE'                     => $user->format_date($row['message_time']),
                        'MESSAGE'                       => $message,
                        'FOLDER'                        => implode($user->lang['COMMA_SEPARATOR'], $row['folder']),
                        'DECODED_MESSAGE'       => $decoded_message,
                        .
                        .
                        
I also added message_text to the create_insert_array function as :

Code: Select all

         public function create_insert_array($pm, $pre_create_data = array())
        {
                $this->set_data('from_user_id', $pm['from_user_id']);

                $this->set_data('message_subject', $pm['message_subject']);

                $this->set_data('message_text',$pm['message_text']);

                parent::create_insert_array($pm, $pre_create_data);
        }
        
cleared the cache and re-tested and still no luck ...

CarolC1
Registered User
Posts: 552
Joined: Sat Dec 02, 2006 4:26 pm

Re: Include PM body in notification email

Post by CarolC1 » Sun Mar 17, 2019 4:43 pm

AlManahry wrote:
Sun Mar 17, 2019 11:55 am
CarolC1 wrote:
Sun Mar 17, 2019 2:04 am
This may help

viewtopic.php?f=456&t=2460446
This works ,but it’s in Beta stage and the developer is not supporting it anymore and not moving it ro production so with any upgrade and change I will have to find anothwr way again :)

Maybe I can look in his code to see the missing lines in my code
Yes, it does say not to use it on a live board. I can tell you it works on our board, but there are differences in servers and differences in what else is installed on a board, so that does not guarantee it will work on every board.

If it doesn't work, an extension should be easy to uninstall, but uninstalling is part of what you would test on a test board.

The author has replied to posts in the beta topic, and I do not see where he is not moving it to production? He is no longer supporting the 3.0 version, and he made a 3.1 version that was never validated, for people who needed it, maybe that is what he was not moving to production. As far as I can tell, the beta extension is almost ready to submit for validation, he just wants more testing to be sure there are no bug reports, maybe you can help?
primehalo Jan 16, 2019 in https://www.phpbb.com/community/viewtopic.php?f=456&t=2460446&start=90#p15180731 wrote:
I was trying to get more people to try it out first before submitting it for official release.

AlManahry
Registered User
Posts: 13
Joined: Tue Mar 12, 2019 5:44 pm

Re: Include PM body in notification email

Post by AlManahry » Sun Mar 17, 2019 9:40 pm

CarolC1 wrote:
Sun Mar 17, 2019 4:43 pm
AlManahry wrote:
Sun Mar 17, 2019 11:55 am
CarolC1 wrote:
Sun Mar 17, 2019 2:04 am
This may help

viewtopic.php?f=456&t=2460446
This works ,but it’s in Beta stage and the developer is not supporting it anymore and not moving it ro production so with any upgrade and change I will have to find anothwr way again :)

Maybe I can look in his code to see the missing lines in my code
Yes, it does say not to use it on a live board. I can tell you it works on our board, but there are differences in servers and differences in what else is installed on a board, so that does not guarantee it will work on every board.
maybe I wasn't clear enough :) , I have it installed .. and it works , but I have two issues :

1- I don't feel such a small feature should be done through lots of coding and an extension ! it really should be a very simple code change .. just I need to know where :)
2- He stopped supporting it :D so what if phpbb came up with this amazing new version with features I need that this extension doesn't work with ? but the native solution is always customize-able , and will get you to know more about phpbb ..

in all cases thanks for the suggestion :) it is a temporary solution ;)

User avatar
mrgoldy
Jr. Extension Validator
Posts: 909
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Include PM body in notification email

Post by mrgoldy » Mon Mar 18, 2019 2:48 pm

First of all, it's not the template variables $template->assign_variables() that are available within the PM notification type, they are explicitly send. It is triggered from this line.

Tracing it back, the message is availabe under the 'message' key. So you'll have to adjust your code in the create_insert_array() function to use that key instead. However, then you will still have a "parsed" text. Which mean it is in XML form and that is not easy on the eyes. So then you have to figure out how you want to display the message in the email. Stripped of tags, rendered as HTML, or what ever form. If you let me know what you want, I can help you out with that aswell.

Code: Select all

public function create_insert_array($pm, $pre_create_data = array())
{
	$this->set_data('from_user_id', $pm['from_user_id']);
	
	$this->set_data('message_subject', $pm['message_subject']);
	$this->set_data('message', $pm['message']);

	parent::create_insert_array($pm, $pre_create_data);
}

Code: Select all

public function get_email_template_variables()
{
	$user_data = $this->user_loader->get_user($this->get_data('from_user_id'));

	return array(
		'AUTHOR_NAME'				=> htmlspecialchars_decode($user_data['username']),
		'SUBJECT'					=> htmlspecialchars_decode(censor_text($this->get_data('message_subject'))),
		'MESSAGE'					=> htmlspecialchars_decode(censor_text($this->get_data('message'))),

		'U_VIEW_MESSAGE'			=> generate_board_url() . '/ucp.' . $this->php_ext . "?i=pm&mode=view&p={$this->item_id}",
	);
}
phpBB Studio / ''Proud member of the Studio"

AlManahry
Registered User
Posts: 13
Joined: Tue Mar 12, 2019 5:44 pm

Re: Include PM body in notification email

Post by AlManahry » Fri Mar 22, 2019 3:23 pm

mrgoldy wrote:
Mon Mar 18, 2019 2:48 pm
First of all, it's not the template variables $template->assign_variables() that are available within the PM notification type, they are explicitly send. It is triggered from this line.

Tracing it back, the message is availabe under the 'message' key. So you'll have to adjust your code in the create_insert_array() function to use that key instead. However, then you will still have a "parsed" text. Which mean it is in XML form and that is not easy on the eyes. So then you have to figure out how you want to display the message in the email. Stripped of tags, rendered as HTML, or what ever form. If you let me know what you want, I can help you out with that aswell.
Perfect as you said I can see the message surrounded by <t></t> , if you can tell me how to get rid of it would be awesome!

so to Summarize , I am running version 3.2.x , to get the text of PM in the notification email the following two functions should be updated as follows in ForumHomeDirectory/phpbb/notification/type/pm.php

Code: Select all

public function create_insert_array($pm, $pre_create_data = array())
{
	$this->set_data('from_user_id', $pm['from_user_id']);
	$this->set_data('message_subject', $pm['message_subject']);
	$this->set_data('message', $pm['message']);

	parent::create_insert_array($pm, $pre_create_data);
}

Code: Select all

public function get_email_template_variables()
{
	$user_data = $this->user_loader->get_user($this->get_data('from_user_id'));

	return array(
		'AUTHOR_NAME'				=> htmlspecialchars_decode($user_data['username']),
		'SUBJECT'					=> htmlspecialchars_decode(censor_text($this->get_data('message_subject'))),
		'MESSAGE'					=> htmlspecialchars_decode(censor_text($this->get_data('message'))),

		'U_VIEW_MESSAGE'			=> generate_board_url() . '/ucp.' . $this->php_ext . "?i=pm&mode=view&p={$this->item_id}",
	);
}
then adding

Code: Select all

{MESSAGE}
to language/en/email/privmsg_notify.txt .

Instead of this native solution you can use primenotify .. however it's worth mentioning that the support is not guaranteed.

I would like to also mention - for the completion of documenting this - that by including the message in the notification , you lose the outbox function of PHPBB where if you decided to delete the message before your receiver reading it you can delete it from outbox , by including the message in the notification though , outbox feature is basically useless.

Thanks everyone and specially mrgoldy .

User avatar
mrgoldy
Jr. Extension Validator
Posts: 909
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Include PM body in notification email

Post by mrgoldy » Fri Mar 22, 2019 3:45 pm

As mentioned, how do you want to display the message in the email? HTML, plain text, other options?
phpBB Studio / ''Proud member of the Studio"

AlManahry
Registered User
Posts: 13
Joined: Tue Mar 12, 2019 5:44 pm

Re: Include PM body in notification email

Post by AlManahry » Fri Mar 22, 2019 3:57 pm

since I can only think of these two options (HTML and Plain text) :D is it OK if you can give me an example on both ? I am also testing substr for PHP to limit the amount of text to let's say 250 characters.. thought of mentioning it in case it might conflict with your solution or if there's better way .. thanks a million !

[UPDATE] trimming the message length working by making the following change :

Code: Select all

'MESSAGE'                               => htmlspecialchars_decode(censor_text(substr($this->get_data('message'),0,250))),


in pm.php

User avatar
mrgoldy
Jr. Extension Validator
Posts: 909
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Include PM body in notification email

Post by mrgoldy » Fri Mar 22, 2019 11:39 pm

For truncating, I would advise to use the truncate_string() function instead: https://github.com/phpbb/phpbb/blob/mas ... .php#L1414
You might have to check if that function is available, otherwise include the file.

Code: Select all

if (!function_exists('truncate_string')
{
	global $phpbb_root_path, $phpEx;
	
	include($phpbb_root_path . 'includes/functions_content.' . $phpEx);
}
Do note that that it is better to inject the root path and php file extension through Symfony service injection, rather than using global on them. Also I would call the/any truncate function last in the chain, otherwise functions that are called afterwards on the text might alter the actual length.

As for "rendering" the text, we either have to use the textformatter's "renderer" class, or the "utils" class. Renderer class is for rendering the text into HTML. The utils class is for creating "plain" text.

This will require a bit more coding so bear with me.
I will both provide the code for the renderer (html text) and the utils (plain text).

After the following line, line 104, add:

Code: Select all

- [set_renderer, ['@text_formatter.renderer']]

Code: Select all

- [set_utils, ['@text_formatter.utils']]
Around line 48 add the following code:

Code: Select all

/** @var \phpbb\textformatter\s9e\renderer */
protected $renderer;

Code: Select all

/** @var \phpbb\textformatter\s9e\utils */
protected $utils;
Around line 58 add the following code:

Code: Select all

public function set_renderer(\phpbb\textformatter\s9e\renderer $renderer)
{
	$this->renderer = $renderer;
}

Code: Select all

public function set_utils(\phpbb\textformatter\s9e\utils $utils)
{
	$this->utils = $utils;
}
Then replace the message declarting:

Code: Select all

'MESSAGE'                               => htmlspecialchars_decode(censor_text($this->get_data('message'))),
with

Code: Select all

'MESSAGE'		=> $this->renderer->render($this->get_data('message'));

Code: Select all

'MESSAGE'		=> $this->utils->clean_formatting(censor_text($this->get_data('message')));
Good luck!
And as a side note, as you're making core code changes, make sure to document all the changes you made to achieve what you're after and save them. Preferably somewhere outside of your phpBB root directory. Because when you update/upgrade you might have to re-do them. This way you atleast know what you have to do!

Cheers,
phpBB Studio / ''Proud member of the Studio"

Post Reply

Return to “phpBB Custom Coding”

Who is online

Users browsing this forum: No registered users and 4 guests