When sending a private message, the notification email contains the HTML characters within the message, including the paragraph tags that are added by CKEditor when the form is submitted.

Whilst these are needed for the message to appear correctly on the site, they need to be converted into plain text when being emailed.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

opdavies created an issue. See original summary.

opdavies’s picture

Issue summary: View changes
opdavies’s picture

Status: Active » Needs review
StatusFileSize
new838 bytes
vandna b’s picture

#3 patch is successfully applied

keshavv’s picture

Status: Needs review » Reviewed & tested by the community
vandna b’s picture

  • Jaypan committed 76e303d on 8.x-1.x
    Issue #2971692 by opdavies, git commit -m Issue
    
jaypan’s picture

Status: Reviewed & tested by the community » Fixed

  • Jaypan committed 76e303d on 8.x-2.x
    Issue #2971692 by opdavies, git commit -m Issue
    
zenimagine’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
Status: Fixed » Active
StatusFileSize
new24.06 KB

The last dev does not correct the problem

jaypan’s picture

Status: Active » Needs work
maaty388’s picture

Status: Needs work » Postponed

I can confirm that this is working on the latest dev version...
Can you please provide more information?

zenimagine’s picture

Yes I installed "Private Message Nodejs". The problem is only with the messages sent and received by the administrator.

zenimagine’s picture

StatusFileSize
new21.82 KB

I changed the text format in the template message but there are still the div tags that appear

PhilippVerpoort’s picture

I can confirm that I've been experiencing the same issue with what's currently the latest release (8.x-2.0-beta15), and it doesn't seem to matter whether messages are sent from the an admin account or not.

opdavies’s picture

Status: Postponed » Needs review
StatusFileSize
new46.08 KB
new57.21 KB
new1.2 KB

I've been still seeing issues when trying to send emails as HTML, where the paragraph tags are appearing from the WYSIWYG field when the token content is replaced.

Attached is a patch that fixes it for me on the 8.x-1.x branch, though I think there's probably a more Drupal-y way to do the last section rather than using str_replace and htmlentities. I don't see the private_message_mail function in the 8.x-2.x branch at all.

zenimagine’s picture

Has anyone found a solution for version 2?

zenimagine’s picture

@Jaypan Hi, do you have a patch to fix this tag problem ?
See my 3 screenshots

zenimagine’s picture

delete

zenimagine’s picture

StatusFileSize
new247.68 KB

The problem is corrected for me.

Just do not check the "Respect provided e-mail format." box in the "SwiftMailer" module.

alternativo’s picture

Hello,
same problem for me (html tags on mail object), solved modifying message_notify.module - function message_notify_mail ()
and message_notify\src\Plugin\Notifier\Email.php customizing the function deliver().
if someone needs details, ask

thanks,
bye

abdelrahman amer’s picture

@alternativo
can u please give me more details about this solution

abdelrahman amer’s picture

StatusFileSize
new512 bytes
alternativo’s picture

Hi Abdelrahman Amer,

this what I did:
message_notify.module:
function message_notify_mail($key, &$message, $params) {
$options = array(
'langcode' => $message['langcode'],
);
switch ($key) {
case 'newmess':
$message['headers'] = $params['headers'];
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('@messsender has sent you a message!', array('@messsender' => $params['mess_sender']), $options);
$message['body'][] = '

' .
t('Hello @userreceiver, @messsender your text! 

', array('@userreceiver' => $params['user_receiver'], '@userpath' => $params['user_path'], '@messsender' => $params['mess_sender']), $options).
'';
break;
}
}

And Email.php in deliver() function:
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'message_notify';
$key = 'newmess';
$to = $mail;

$params['headers']['MIME-Version'] = '1.0';
$params['headers']['Content-Type'] = 'text/html; charset=UTF-8';
$params['headers']['Content-Transfer-Encoding'] = '8Bit';

$currentusername = \Drupal::currentUser()->getUsername();
$params['user_path'] = 'http://yoursite.com' . \Drupal::currentUser()->id();
$params['mess_sender'] = $currentusername;
$receiver = user_load_by_mail($mail);
$params['user_receiver'] = $receiver->getUsername();

$langcode = $receiver->getPreferredLangcode();

$send = true;
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);

Hope it helps

sagesolutions’s picture

StatusFileSize
new753 bytes

I tried patch #23, but <p>&nbsp;<p> results in &nbsp;

I've added different solution that instead of showing the html, it renders the html.

https://drupal.stackexchange.com/questions/222201/how-can-i-display-toke... for reference.

hoporr’s picture

We had the same problem as #25, and the patch in #25 worked well, for version 3.0.0-beta.

sagesolutions’s picture

Version: 8.x-2.x-dev » 3.0.x-dev
jphelan’s picture

Patch #25 works for me on 3.0.1.

dxvargas’s picture

I have a question about the patch #25 and the usage of Drupal\Core\Render\Markup::create. Is it safe?
I'm afraid we are marking the message sent by the user as safe and sending it as is in the email.
Can someone please confirm or rebut this?

jaypan’s picture

usage of Drupal\Core\Render\Markup::create. Is it safe?

I don't think it is. From the Markup documentation:

This object should only be constructed with a known safe string. If there is any risk that the string contains user-entered data that has not been filtered first, it must not be used.

jphelan’s picture

Yeah, I think you guys are right, I think we need to use Xss::filter() on it. I've created a new patch to allow only a few safe HTML tags.

herved’s picture

Status: Needs review » Needs work

Can't we simply use $private_message->get('message')->processed ?
This uses \Drupal\text\TextProcessed::getValue and returns a FilteredMarkup which would align with the filter format. It would align with the filter used while Xss::filter may filter way more than expected.

claudiu.cristea’s picture

Agree with #33

zaryab_drupal’s picture

StatusFileSize
new514 bytes
claudiu.cristea’s picture

Issue tags: +Needs tests

We need a test to prove that text from email is not HTML-encoded. Also, please use the MR, with patches there's no test run

claudiu.cristea’s picture

  • claudiu.cristea committed c496aa62 on 3.0.x
    Issue #2971692 by claudiu.cristea, jphelan, opdavies, Abdelrahman Amer,...
claudiu.cristea’s picture

Status: Needs work » Fixed
Issue tags: -Needs tests

Thank you all

herved’s picture

Status: Fixed » Closed (fixed)

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