Hi

i would to theme the webform-mail.tpl...so i copy it in my theme, but how to insert html code into php ? and how to know the value name detail ? is it the name displayed in editing/creation of component ??

here i am actually :

<?php print_r($submission) ?>
<?php print "<div id='corps-email'>"//debut div corps du mail?>
<?php print t('Submitted values are') ?>:

%email_values
<?php print "</div>"//fin corps du mail?>
<?php //print t('The results of this submission may be viewed at:') ?>

%submission_url

but print-r doesnt work, and the other part of code doesnt appear in the source code of my email..

thanks for helping

Comments

aiphes’s picture

so i've changed my code but still no result in email received instead of the source.

<p>
<span class="titre_email">%email[1] : </span> %value[1] <br>
<b>%email[2] : </b> %value[2] <br>
<b>%email[3] : </b> %value[3] <br>
<b>%email[4] : </b> %value[4] <br>
<b>%email[5] : </b> %value[5] <br>
<b>%email[6] : </b> %value[6] <br>
<b>%email[6_0][6_1] : </b> %value[6_0][6_1] <br>
<b>%email[6_0][6_2] : </b> %value[6_0][6_2] <br>
<b>%email[6_0][6_3] : </b> %value[6_0][6_3] <br>
<b>%email[6_0][6_4] : </b> %value[6_0][6_4] <br>
<b>%email[7] : </b> %value[7] <br>
<b>%email[8] : </b> %value[8] <br>
<b>%email[9] : </b> %value[9] <br>
<b>%email[10] : </b> %value[10] <br>
<b>%email[11] : </b> %value[11] <br>
<b>%email[12] : </b> %value[12] <br>
<b>%email[13] : </b> %value[13] <br>
<b>%email[14] : </b> %value[14] <br>
<b>%email[15] : </b> %value[15] <br>
<b>%email[16][16_1] : </b> %value[16][16_1] <br>
</p>

someone can help me to  make it work please ?

thanks

EDIT: is there need to setup something for html email format ? because in thunderbird the html doesnt appear in source code..i've setup html format for all webform forms in general settings of webform...i dont know what to do more...help

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

aiphes’s picture

so the tpl.php doesnt work for me; i put the code at the bottom of the page webform/email...but i dont know how to do to get the label in black and the answer in single text...because
- %email[2] give me the ask and the answer..
- %value[2] give me the answer..

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

aiphes’s picture

nobody knows how to separate question and the answer ( key and value) ?? its the final problem that i ve...please some help

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

malberts’s picture

Hi aiphes

Here's a rough idea of what you should do. When I've got some more time I'll try to turn this into a Documentation page with some more examples of cool stuff you can do with Webform emails.

Preliminaries

  1. Install Mime Mail.
  2. Go to admin/settings/mimemail and make sure that the filter is set to the correct one. If you want full HTML in your email (I assume that you do) then select the applicable filter, Full HTML most likely.
  3. Go to the webform email settings page, e.g. node/32/webform/emails, and edit the addresses you want to send HTML mails to. Tick "Send e-mail as HTML" in the E-Mail template section.

Webforms should now be set up to send HTML emails, but you'll want to edit the template file to make it do interesting things.

Template Setup

  1. In your filesystem, copy modules/webform/templates/webform-mail.tpl.php to your theme's template directory.
  • If you keep the filename as webform-mail.tpl.php then the template will apply to all your webforms.
  • To create a custom email for each webform you need to have a template file for each webform. They will be renamed webform-mail-[nid].tpl.php where [nid] is the node id of the webform.

    E.g. webform-mail-6.tpl.php for the webform with node id 6.

Template Content

A few notes on what you'll see in the original tpl file:

  • print ($email['html'] ? '<p>' : '')
    • This is used to generate HTML tags if the webform is set to contain HTML. If you know for sure that your email will always be in HTML then you don't need to use this.
    • If you do need to perform this check you will have to place this code wherever you want HTML tags. If you have to use a lot of HTML you might want to consider doing a big IF-ELSE that checks $email['html'] once and generates the HTML version on the IF and a plain text one on the ELSE. This is your choice.
  • %submission_url
    • The %-variables are used by Webform to output "Label: Value"-pairs. You don't really want this because it prevents you from injecting HTML around the Label and Value separately.
    • Don't use them.
  • While in this template file you can do anything you could do in code elsewhere. This means that you can have much more in this template file than what you are given by the variables mentioned in the template file. E.g. you can load other nodes, user info, output views, whatever you want. Note: I don't know how much you need to do to degrade Webform performance, so just keep that in mind. Just styling the output shouldn't have any effect, though.
  • You can play around with print_r to get the data structure and see what can be done. I'll give a few examples below.
  • You'll want to use if($submission) before you do anything special. This tests if the webform has got any submitted content. Otherwise you'll get an error because $submission is empty on the Webform "Edit e-mail settings" page. You'll also prevent unnecessary calculations and formatting from being done that won't be displayed on that page.

Example

Looping through all the fields and outputting in a table

	$col_left = 175;
	$col_right = 475;
	$col_tot = $col_left + $col_right;
	
	global $base_url;

	if($submission){
		print ($email['html'] ? '<table align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; width:'.$col_tot.'px;">' : '');
		print ($email['html'] ? '<tbody style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">' : '');
		
		//Title
		print ($email['html'] ? '<tr style="font-family:Arial, Helvetica, sans-serif; font-size:16px; font-weight:bold;">' : '');
		print ($email['html'] ? '<td colspan=2 style="font-family:Arial, Helvetica, sans-serif; font-size:16px; font-weight:bold; vertical-align:top;">' : '');
		print t('My Website Online Enquiry</br>&nbsp;');
		print ($email['html'] ? '</td>' : '');
		print ($email['html'] ? '</tr>' : '');
	
		//Date		
		print ($email['html'] ? '<tr style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">' : '');
		print ($email['html'] ? '<td style="font-family:Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; vertical-align:top; width:'.$col_left.'px;">' : '');
		print t('Enquiry Date').':';
		print ($email['html'] ? '</td>' : '');
		
		print ($email['html'] ? '<td style="font-family:Arial, Helvetica, sans-serif; font-size:12px; vertical-align:top; width:'.$col_right.'px;">' : '');
		print t('!date', array('!date' => date('Y/m/d, H:i', $submission->submitted)));
		print ($email['html'] ? '</td>' : '');
		print ($email['html'] ? '</tr>' : '');
		
		foreach($node->webform['components'] as $eid => $entry){
			if($entry['type']=='fieldset'){
				print ($email['html'] ? '<tr style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">' : '');
				print ($email['html'] ? '<td colspan=2 style="font-family:Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; vertical-align:top;">&nbsp;<br/>' : '');
				print $entry['name'];
				print ($email['html'] ? '</td>' : '');
				
				print ($email['html'] ? '</tr>' : '');
			}else{
				if($submission->data[$eid]['value'][0]){
					print ($email['html'] ? '<tr style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">' : '');
					
					print ($email['html'] ? '<td style="font-family:Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold; vertical-align:top; width:'.$col_left.'px;">' : '');
					
					print $entry['name'].':';
					print ($email['html'] ? '</td>' : '');
					
					print ($email['html'] ? '<td style="font-family:Arial, Helvetica, sans-serif; font-size:12px; vertical-align:top; width:'.$col_right.'px;">' : '');
					foreach($submission->data[$eid]['value'] as $vid => $value){ 
						print nl2br($value);
					} 
					print ($email['html'] ? '</td>' : '');
					
					print ($email['html'] ? '</tr>' : '');
				}
			}
		}

		$options = array(
			'attributes' => array(
				'style' => 'font-family:Arial, Helvetica, sans-serif; font-size:12px;',
			),
		);
		
		print ($email['html'] ? '<tr style="font-family:Arial, Helvetica, sans-serif; font-size:14px;">' : '');
		print ($email['html'] ? '<td colspan=2 align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; vertical-align:top;">&nbsp;<br/>' : '');
		print l('View enquiry online',url('node/'. $node->nid .'/submission/'. $submission->sid, array('absolute' => TRUE)), $options);
		print ($email['html'] ? '</td>' : '');
		print ($email['html'] ? '</tr>' : '');
		
		print ($email['html'] ? '<tr style="font-family:Arial, Helvetica, sans-serif; font-size:14px;">' : '');
		print ($email['html'] ? '<td colspan=2 align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; font-weight:bold; vertical-align:top;">&nbsp;<br/>' : '');
		print l('Go to My Drupal Website',$base_url.base_path(), $options);
		print ($email['html'] ? '</td>' : '');
		print ($email['html'] ? '</tr>' : '');
		
		print ($email['html'] ? '</tbody>' : '');
		print ($email['html'] ? '</table>' : '');
	}
aiphes’s picture

thanks a lot ! great explanation...when i go to theme a new webform mail it will be useful :)
i use mimemail actually and its a better way to read email...so after that i'll try your way

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

rolkos’s picture

Ok, but what should I do with file fields? And another thing, I have list of countrys like:

PL|Poland
UK|England
US|United States

when I use something like this:

print ($submission->data[12]['value'][0]);

Im getting only key name not regular name.

malberts’s picture

Hi rolkos

Do a print_r of $node and $submission and paste the output here (you may trim everything not related to the file field and the country list field). I haven't used either of those so I'll have to see the data structure to help you.

File Field

In my example I used if($entry['type']=='fieldset'){ to test for a fieldset, so you could just add an else if and check for a filefield (or turn it into a switch if you're going to check lots of other fields). I'm not sure what the filefield's type is named, however. You should be able to find that in the print_r. To print a link to the file you would use

//Note: I use hardcoded styling because they are more reliable in email clients. You dont have to use this.
//If you don't use this, omit the $options variable below and in the l() call.
$options = array(
            'attributes' => array(
                'style' => 'font-family:Arial, Helvetica, sans-serif; font-size:12px;',
            ),
        );
print l($filefield_name,$filefield_url, $options);

$filefield_name is a human readable name if that exists (I don't think so?), otherwise use "Download File" or something else in the print_r.
$filefield_url is the url you'll get in the print_r.

List Values

The key name is returned in the $submission variable so you'll have to look up the actual name in the $node variable. Make another else-if, like above, but since you'll be looking up a value of a specific field you need to put another if in there to test whether it's the correct field (using the $eid variable in my example), e.g.

if($entry['type']=='fieldset'){
    //print fieldset-specific code
}else if($entry['type']=='filefield'){
    //print filefield-specific code
}else if($entry['type']=='list'){
    if($eid==$your_country_list_id){
        //print the looked-up country names
    }
    //
}else{
    //print other fields
}
Saoirse1916’s picture

Just to add to malberts' post, I had to deal with this same problem and this is how I solved it though I am curious if there's a more efficient solution. I'm no PHP expert so it wouldn't surprise me if there were a better way.

Since select fields use a \r for a row delimiter and a pipe for a column delimiter, I split the string into an array, then cycled the array looking for a match:

<?php
}else if($entry['type']=='select'){
	if ($entry['extra']['items']){
		//create an array from the string of all possible values
		$list = explode("\r",$entry['extra']['items']);
		//cycle the array
		foreach($list as $list_item) {
			//find the pipe and the key/value on either side of it
			$pos = strpos($list_item,'|');
			$key = substr($list_item,0,$pos);
			$value = substr($list_item,($pos + 1));
			//if there is a key match, write the value
			if(trim($key) == $submission->data[$eid]['value']['0']) {
				print nl2br(trim($value));
				break;
			}
		}
	}
?>

Hope this helps someone save some time!

benfreda’s picture

Wow, thanks a ton for this, it definitely saved me some time.

One thing I had to do differently was on lines 3-5 of your code:

        //create an array from the string of all possible values
        $list = explode("\r",$entry['extra']['items']);

Instead, I had to split along \n, not \r, so it was:

        //create an array from the string of all possible values
        $list = explode("\n",$entry['extra']['items']);

Otherwise, worked like a charm. Thanks.