Force line-breaks/word-wrap for e-mails to prevent lines of HTML being sent that exceed ~990 characters.

Created on 15 January 2025, 4 days ago

This is the first time I've ever submitted anything like this, so I apologize if it's formatted incorrectly or anything.

Problem/Motivation

My use-case is a long webform that has quite a few input boxes with relatively unrestricted character limits. I've created an e-mail handler with a custom body/message to send the results to an e-mail address, but even though I experimented with using
, CSS, and alternate ways to break lines from within the Webforms module, it still resulted in sending super long lines of HTML to the php-mail function which caused line breaks with a "!" at character 990 (which would end up rendered in the final e-mail).

Steps to reproduce

Here is a snippet of my custom-body for the e-mail handler:

<p>
    <strong>Submitted on:</strong> [webform_submission:created]
</p>
<p>
    <strong>Submitted by:</strong> [webform_submission:user]
</p>
<p>
    <strong>[webform:element:faculty_information:title]</strong>
</p>
<p>
    [webform_submission:values:faculty_information]
</p>
<p>
    &nbsp;
</p>
<p>
    <strong>Faculty Professional Development Plan:</strong>
</p>
<p>
    <strong>[webform:element:a_goals_plans_for_summer_2024_fall_2024_and_spring_2025:title]</strong>
</p>
<p>
    [webform_submission:values:a_goals_plans_for_summer_2024_fall_2024_and_spring_2025]
</p>
<p>
    <strong>[webform:element:b_long_term_goals_beyond_spring_2025:title]</strong>
</p>
............

This caused the e-mail to be sent to have lines with 990 characters in them, broken by a "!" (Lorem Impsum text used for testing purposes):

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Webform submission from: Faculty Professional Development Plan and Annual Report</title>
</head>
<body>
<p><strong>Submitted on:</strong> Wed, 01/15/2025 - 08:49</p><p><strong>Submitted by:</strong> MicahShenk</p><p><strong>Faculty Information</strong></p><p></p><table data-striping="1" class="table">
  
  
  
      <tbody>
                      <tr class="odd">
                      <td>Faculty Member:</td>
                      <td>Oratione</td>
                  </tr>
                      <tr class="even">
                      <td>Faculty Email Address:</td>
                      <td>10 Main Street@dfgh.dfg</td>
                  </tr>
                      <tr class="odd">
                      <td>Department:</td>
                      <td>Business of Art &amp; Design</td>
                  </tr>
          </tbody>
    </table>
<p>&nbsp;</p><p><strong>Faculty Professional Development Plan:</strong></p><p><strong>A. Goals &amp; Plans for Summer 2025, Fall 2025, and Spring 2026:</strong></p><p>Quae cum dixisset, finem ille. Quamquam non negatis nos intellegere quid sit voluptas, sed quid ille dicat. Progredientibus autem aetatibus sensim tardeve potius quasi nosmet ipsos cognoscimus. Gloriosa ostentatio in constituendo summo bono. Qui-vere falsone, quaerere mittimus-dicitur oculis se privasse; Duarum enim vitarum nobis erunt instituta capienda. Comprehensum, quod cognitum non habet? Qui enim existimabit posse se miserum esse beatus non erit. Causa autem fuit huc veniendi ut quosdam hinc libros promerem. Nunc omni virtuti vitium contrario nomine opponitur.</p><p><strong>B. Long Term Goals (beyond Spring 2026):</strong></p><p>Huius, Lyco, oratione locuples, rebus ipsis ielunior. Duo Reges: constructio interrete. Sed haec in pueris; Sed utrum hortandus es nobis, Luci, inquit, an etiam tua sponte propens! <-- LINE BREAK HERE
 us es? Sapiens autem semper beatus est et est aliquando in dolore; Immo videri fortasse. Paulum, cum regem Persem captum adduceret, eodem flumine invectio? Et ille ridens: Video, inquit, quid agas;</p><p>&nbsp;</p><p><strong>Faculty Annual Report</strong></p><p></p><table class="responsive-enabled table" data-striping="1">
.............

Proposed resolution

Force a wordwrap() on the message's body before sending the e-mail. I have tested this on my setup and it works--but have not done further testing.
In src/Plugin/WebformHandler/EmailWebformHandler.php, line 1166-1167 (in 6.2.7 at least) becomes:

unset($message['parameters']);
$message['body'] = wordwrap($message['body'], 75, "\n");
$result = $this->mailManager->mail('webform', $key, $to, $current_langcode, $message, $from);

This does not appear to visually alter the final e-mail in any way.

Remaining tasks

Test to make sure this doesn't break anything else.

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

6.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bugiboop

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

Comments & Activities

Production build 0.71.5 2024