Hi,

When configuring the email template that gets generated by webforms, I intended to use %email_values to include most of my fields directly into the email. Due to the fact that webform provides a section where I can tick the fields I would like to have included in the %email_values token, this is considerably faster than needing to add individual fields to the email template with %value[key].

However, the formatting of %email_values seems wrong. All my fields were mushed up in the same line in the email. Does anybody else experience this issue? Shouldn't each field be in a separate line at least?

Thanks, J.

Comments

Odd Hill’s picture

I have the exact same problem, and haven't found a solution yet. I'm not using MIME mail module because I don't want all that overhead, so I'm using a function added to tempate.php i order for it to send HTML emails.

quicksketch’s picture

Category: bug » support

I have the exact same problem, and haven't found a solution yet. I'm not using MIME mail module because I don't want all that overhead, so I'm using a function added to tempate.php i order for it to send HTML emails.

If you're not telling Webform you're sending HTML e-mails, Webform will not automatically add the necessary tags (<p></p> wrapped around each line) and %email_values will show up all on one line. The only way to use %email_values and manually send HTML e-mails is likely to use hook_mail_alter() and run nl2br() on the e-mail before it goes out, converting every new line into a <br /> tag.

jelo’s picture

If you're not telling Webform you're sending HTML e-mails
How do I actually tell webform what type of emails I want to send? I was under the impression I am sending plain text emails.

Under /admin/settings/webform I could only find the option to select between plain text and HTML format as well as this note: "The default format for new e-mail settings. Webform e-mail options take precedence over the settings for system-wide e-mails configured in MIME mail."

I just checked the full headers of the message that gets currently generated:
MIME-version: 1.0
X-Mailer: Drupal Webform (PHP/5.2.6-3ubuntu4.4)
Content-type: text/plain; charset=UTF-8; format=flowed; delsp=yes
Content-transfer-encoding: 8Bit

Even when I send in plain text I should be able to get every field into a separate line by adding \n\r for line breaks, right?

quicksketch’s picture

I'm not quite sure why you're getting everything on one line. Perhaps MIME Mail is sending the e-mail in a format in which new lines don't have an effect? In your case I'm not sure why things are appearing on one line if the e-mail being sent is already plain text. When you look at the raw source of the e-mail, the new lines are all in place there, correct?

jelo’s picture

Status: Active » Closed (cannot reproduce)

Nathan, I just went back to test it, ie. got rid of the individual %value[key] fields and used %email_values, and strangely enough the problem has disappeared. Now the email is well formatted, each field in a new line and the token includes exactly the values I ticked under "included email values".

Not sure what happened after the upgrade, but it initially did not work this way.

Thanks for the quick replies.

Cheers, J.

StephenRobinson’s picture

I personally find %email_values in webform-mail.tpl.php very irritating as doesn't allow proper theming in an easy fashion, each of the values should be modifiable via a theme template. To get around this I am going to remove %email_values, and add a function to generate my own hash, this can be generated with the following code:

if(is_object($node)){
  $custom_email_values=array();
  foreach($node->webform['components'] as $key => $value){
    $subvalue = '';
    $subvalues=array();     
    if(is_array($submission->data[$key]['value'])){
      foreach($submission->data[$key]['value'] as $key2 => $value2){
        $subvalues[$key2]=$value2;
      }
      $subvalue = implode($subvalues, ', ');
    }
    $custom_email_values[$key]=array(
      'name' => filter_xss_admin($value['name']), 
      'value' => $subvalue, 
    );
  }
}
drupal_set_message('<pre>'.htmlspecialchars(print_r($custom_email_values,1)).'</pre>');
StephenRobinson’s picture

Actually, as all I wanted was to double space, I settled for this:

/**
 * Double space %email_values in webform emails
 */
function mymodule_webform_submission_render_alter(&$renderable) {
  foreach($renderable['#node']->webform['components'] as $key => $value){
    $renderable['#node']->webform['components'][$key]['name']='
'.$renderable['#node']->webform['components'][$key]['name'];
  }
}
druwebdev11’s picture

When replacing %email_values with the above code, I see no change in the formatting of the email received. Any ideas?

druwebdev11’s picture

Component: Miscellaneous » Code
Priority: Normal » Major

When replacing %email_values with the above code, I see no change in the formatting of the email received. Any ideas?

tmsimont’s picture

I've got a clean install of webform and see the same problem. I have not done anything to modify the email template, which looks like this:

default template:

Submitted on %date
Submitted by user: %username
Submitted values are:
%email_values

The results of this submission may be viewed at:
%submission_url

The line breaks between the "Submitted by" and "Submitted by user:" work fine, but %email_values puts a bunch of mushed together key/value pairs into a single line.

tmsimont’s picture

Status: Closed (cannot reproduce) » Active
quicksketch’s picture

I've got a clean install of webform and see the same problem. I have not done anything to modify the email template, which looks like this:

This is an absolutely clean install of Webform? Can you reproduce this problem all the way from a clean install of Drupal? Knowing how to reproduce the problem would help me find out where this is coming from.

tmsimont’s picture

i may have had a few other modules active, but I had just installed and activated webform when i ran into the issue. i'm not 100% sure anymore that it was a perfectly clean install, but i know that i've since installed a handful of webform related modules.... i'll have to do some backtracking and confirm that i didn't have anything that might have been a source of conflict.

one thing to note -- this may be an email client thing. this is what gmail gets before it renders (available in gmail when you click "Show original"):


This is a multi-part message in MIME format...

------------=_1324672800-5184-1
content-transfer-encoding: quoted-printable
Content-Type: text/plain; charset="utf-8"

=0D
Submitted on Friday, December 23, 2011 - 20:39=0D
Submitted by user: admin=0D
Submitted values are:=0D
=0D
Location: Halifax=0D
First Name: Test=0D
Last Name: Tester=0D
Company Name:=0D
Address:=0D
Email: test@example.com=0D
Phone:=0D
Comments/Questions: test=0D
=0D
=0D
The results of this submission may be viewed at:=0D
http://example.com/node/862/submission/30=0D
=0D


------------=_1324672800-5184-1
Content-Type: text/html
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

<html><body>
<p>Submitted on Friday, December 23, 2011 &ndash; 20:39 Submitted by user: =
admin Submitted values are:</p>
<p>Location: Halifax First Name: Test Last Name: Test=
er Company Name: Address: Email: test@example.com Phone: Comments/=
Questions: test</p>
<p>The results of this submission may be viewed at: <a href=3D"http://example.com/node/862/submission/30">http://example.com/node/862/submission/30</a></p>
=0D
<img src=3D"http://u42353.sendgrid.org/wf/open?rp=3DftCS7aD2B1CdKPmhuiLR1dh=
jfKx3vxmmS6Jtz6MxsZGIMbDz0hVM%2BVj1RkFVIMtrAUk0OqHOwUYGbsuQDbJIJZIdZsCLLixt=
7Y%2FQP2bX3Jkvv24bi1UJoWHVSpVs10vk&amp;u=3Ddoa-49UyR4OfhvQhumRLIg%2Foo0.gif=
" alt=3D"">=0D
</body></html>=0D

------------=_1324672800-5184-1--

It looks like the email is sent with line breaks, but also sent as an HTML email with <p> tags. I'll see if I can reproduce this again with an absolutely fresh install, but does that email look right?

quicksketch’s picture

It looks like the email is sent with line breaks, but also sent as an HTML email with

tags. I'll see if I can reproduce this again with an absolutely fresh install, but does that email look right?

Webform can't send HTML-based or multi-part e-mails unless you've installed MIME Mail module. If you haven't installed MIME Mail, then another module is modifying your e-mails (likely site-wide) to format them as HTML.

tmsimont’s picture

Thanks quicksketch -- i looked into the other modules i've got and one of the first things i'd installed was smtp (http://drupal.org/project/smtp)

I've got that rigged to use sendgrid (sendgrid.com) and that's where the emails get converted to HTML.

unfortunately my server is not able to send emails without this and although I see that in sendgrid there's a way to prevent the conversion to HTML, I don't have permission (within my company) to change this setting. I'll see if I can get the bossman to change that, and i'll report back the results.

does MIME mail properly generate HTML emails (by properly i mean does it insert break tags or close p tags around line breaks)?

quicksketch’s picture

does MIME mail properly generate HTML emails (by properly i mean does it insert break tags or close p tags around line breaks)?

MIME Mail itself is capable of taking plain-text e-mails and converting them to HTML, yes. But if MIME Mail is available, Webform will detect it and format it's e-mails as HTML from the beginning, essentially making the web browser display identical to what's in the e-mail (it's actually the exact same markup in both places). So if you took that route, I imagine that SendGrid wouldn't attempt to do the conversion for you, since the e-mail will already be HTML by the time it gets it. I can't say for sure though, I've never tried to use that service (though it looks interesting).

jjmontgo’s picture

Hi!

A similar solution which doesn't work was posted earlier. This one does. I also added a loop over fieldsets to separate out the components in each fieldset.

function modulename_webform_submission_render_alter(&$renderable) {
  /**
   * Double space %email_values in webform emails
   */
    foreach (element_children($renderable) as $key) {    
      $renderable[$key]['#title'] = "\n" . $renderable[$key]['#title'];
      if (isset($renderable[$key]['#webform_component']['type']) && $renderable[$key]['#webform_component']['type'] == 'fieldset') {
        $fieldset = &$renderable[$key];      
        foreach (element_children($fieldset) as $key2) {
          $fieldset[$key2]['#title'] = "\n" . $fieldset[$key2]['#title'];
      }
    }
  }
}
tmsimont’s picture

I imagine that SendGrid wouldn't attempt to do the conversion for you, since the e-mail will already be HTML by the time it gets it. I can't say for sure though, I've never tried to use that service (though it looks interesting).

I tried it out and SendGrid does in fact recognize the HTML in the emails sent by MIME Mail. Nice integration with that module, btw. Everything is working great now. thanks for your help.

kt-designs’s picture

I was also having the wrapping issues (we use gmail for our company email & we're only sending plain text emails). After reading tmsimont's comment about the email client, I added a non-gmail account to my set of email address. The non-gmail email had no wrapping issues.

This may help others:
I did notice that items in fieldsets did not have wrapping issues. So, I just added the section of items that wrapped in a new fieldset. Problem solved.
Kathy

quicksketch’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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

lancewig’s picture

Solution #17 by jjmontgo was perfect solution for webform on Drupal 7 site.

Wasted another hour before finding this post.

bearstar’s picture

Confirmed. #17 works like a charm on D 7.

NaX’s picture

Issue summary: View changes
Status: Closed (fixed) » Needs review

Ok, I think this is an Outlook issue where the auto remove link breaks feature sometimes and sometimes not removes the line breaks in parts of plain text emails.

One work around if found was to append a new line and two spaces in front of every line in the email, like below. This fixed the problem for me.

You can see it being discussed here:
http://www.masternewmedia.org/newsletter_publishing/newsletter_formattin...

/**
 * Implements hook_mail_alter().
 */
function mymodule_mail_alter(&$message) {
  if ($message['module'] == 'webform') {
    foreach ($message['body'] as $k => $v) {
      $body_pieces = explode("\n", $v);
      $body_pieces = array_map('trim', $body_pieces);
      $line_end = "\n" . '  ';
      $message['body'][$k] = implode($line_end, $body_pieces);
    }
  }
}

I hope that helps.

DanChadwick’s picture

Priority: Major » Normal
Status: Needs review » Closed (fixed)

Thanks for the tip. There is no patch to review here and D6 receives only critical fixes at this point.

NaX’s picture

@DanChadwick
D6 / D7, it does not matter as the problem is a Outlook email client filter that removes line breaks. The above code was taken from a D7 site.

nsnoblin’s picture

I'm having this problem, not related to Outlook, and #17 didn't work. Webform emails were fine until I started using SendGrid with the SMTP Authentication Support and Mail System modules.

DanChadwick’s picture

Re #27 -- it seems there are two issues being discussed here. If you are having an issue with the 7.x-4.x branch, please open a new issue with a detailed description of the issue and how to reproduce it.

flyke’s picture

I found a solution for the same problem with webforms in Drupal 7.

My site uses the htmlmail mail system which does not provide a checkbox in a webform mail settings to set the format to html. I can format the entire mail as html via a template, but [submission:values] will always render everything in 1 line.

Since this page often shows up as first result in search engines, I decided to share my solution here to have every submisison value on a new line.

Solution:
- In your theme folder, inside your templates folder, create a file with the following filename: webform-submissions.tpl.php
- Inside that file, place the following php code:

<?php

if(is_array($renderable)){
  foreach($renderable as $key => $value){
    if(is_array($value) && isset($value['#value'])){
      $renderable[$key]['#value'] .= '<br/>';
    }
  }
}
print drupal_render_children($renderable);
?>

- clear caches and that's it :-)

oranges13’s picture

Instead of potentially clobbering the value, it would most likely be better to add a suffix value.

$renderable[$key]['#suffix'] = "\n";

markabur’s picture

There is a setting in SendGrid that worked for us -- just turn on the "Plain Content" option. This setting says, "Turn on if you don't want to convert your plain text email to HTML."