Ok, a bit of a long post but this should get you going... You’re well on your way with what you have there. The only thing missing from your form template as you posted would be the toSend field. Normally you’d have something like this somewhere in your form:
<dt><label accesskey="s" for="cfToSend">Recipient:</label></dt>
<dd><select name="toSend" id="cfToSend" eform="Recipient::1">
<option value="1">Sales</option>
<option value="2">Support</option>
<option value="3">Billing</option>
</select></dd>
This should be all you need to create a generic contact form where the user can select the recipient. However we can enhance this so you could have links such as this on your site leading to the same form without displaying the recipient dropdown (select) box.
<a href="contact.html?mailto=3">Contact our Billing Department</a>
To do this you need to first of all create an extra snippet, call it
contactFormFunctions and paste the below script:
<?php
function onBeforeParseFunction(&$fields,&$templates){
global $modx;
//fetch the email addresses from the eform parameters
$addresses = explode(',', $modx->event->params['to']);
//add dummy to correct index
array_unshift($addresses,$addresses[0]);
//make sure this array contains (at least) the same number of elements as you have
//addresses in the &to parameter
$select_subjects = array(
1 => 'Sales',
2 => 'Support',
3 => 'Billing'
);
$email_index = 0; //initialize
if( isset($_GET['mailto']) ){
$email_index = abs($_GET['mailto']); //array index is 1 lower then email index
$_SESSION['eform_mailto'] = $email_index; //remember value
}elseif( isset($_SESSION['eform_mailto']) )
$email_index = $_SESSION['eform_mailto'];
if( $email_index > 0 ){
//make sure address index exists or make it the first one in the list
$email_index = ( isset($addresses[$email_index]) ) ? $email_index : 1;
$mail_tag = '<dt><input type="hidden" name="toSend" value="'.$email_index.'" /></dt>';
}else{
//Display a select tag with all addresses
$mail_tag = '
<dt><label accesskey="s" for="cfToSend">Recipient:</label></dt>
<dd><select name="toSend" id="cfToSend" eform="Recipient::1">';
for($i=1;$i<(count($addresses));$i++){
//just in case there's a discrepancy between subjects array and email addresses
$subject = ( isset($select_subjects[$i]) ) ? $select_subjects[$i] : ucfirst(implode(' ',explode('.',substr($addresses[$i],0,strpos($addresses[$i],'@')))));
$mail_tag .= "\n\t". '<option value="'.$i.'">'.$subject.'</option>';
}
$mail_tag .= '</select></dd>';
}
$templates['tpl'] = str_replace('[+mailaddress_tag+]',$mail_tag,$templates['tpl']);
return true;
}
function onBeforeMailFunction( &$fields ){
//successfully submitted form so destroy session variable
unset($_SESSION['eform_mailto']);
return true;
}
return ''; //return empty value from snippet
?>
Next you need to adjust your form template and instead of the select tag insert a
[+mailaddress_tag+] placeholder. This will be replaced with either a select tag or a hidden input tag, depending on how you arrive at the contact form. More about that in a moment. So your form template should now look like this:
[+validationmessage+]
<form method="post" action="[~[*id*]~]" id="contactForm" >
<fieldset>
<p class="legend">Contact form:</p>
<dl class="details">
<dt> <input name="formid" type="hidden" value="contactForm" /> </dt>
[+mailaddress_tag+]
<dt><label for="cfName">Full Name:</label></dt>
<dd><input name="name" id="cfName" type="text" size="30" eform="Full name::1:" /></dd>
<dt><label for="cfEmail">Email: </label></dt>
<dd><input name="email" id="cfEmail" type="text" size="30" eform="Email Address:email:1" /></dd>
<dt><label for="cfTelephone">Telephone:</label></dt>
<dd><input name="tel" id="cfTelephone" type="text" size="30" eform="Telephone::1:"/></dd>
<dt> <label for="cfMessage">Message:</label></dt>
<dd><textarea name="Message" cols="35" rows="6" id="cfMessage" eform="Message:textarea"></textarea></dd>
</dl>
</fieldset>
<p><input type="submit" name="submit" value="Send Feedback"></p>
</form>
The last thing you need to do is to add a snippet call to the new
contactFormFunctions snippet and to adjust your snippet call for including the eFormOnBeforeFormParse and the eFormOnBeforeMailSent events. Here’s the new calls:
(
I’ve placed the parameters on separate lines for clarity but you must have all parameters on one line when copying/pasting - as you had before)
[[contactFormFunctions]]
[!eForm?
&formid=`contactForm`
&subject=`[Contact Form]`
&tpl=`contactForm`
&report=`contactReport`
&gotoid=`40`
&sendAsHtml='1'
&replyto=`email`
&to =`[email protected],[email protected],[email protected]`
&mailselector=`toSend`
&eFormOnBeforeFormParse=`onBeforeParseFunction`
&eFormOnBeforeMailSent=`onBeforeMailFunction`
!]
This should now allow you to have links to the contact form like this:
contact.htm -> shows contact form with a recipient dropdown list
contact.htm?mailto=3 -> contact form without dropdown list and preset to 3rd email address (billing)
etcetera.
To increase the number of recipients in the list you need to adjust the &to parameter and add elements to the
$select_subjects array in the
onBeforeParseFunction of the
contactFormFunctions snippet. The
$select_subjects array looks like this:
<?php
$select_subjects = array(
1 => 'Sales',
2 => 'Support',
3 => 'Billing'
);
?>
As you said you’re not a php coder, Just remember to add comma’s after each array element except the last one!
Good luck