The ’to’ that you’re referring to is not a form field but a snippet parameter (it’s not in the $fields array). You don’t have access to the snippet parameters in your function but there is a way to get around that in this case.
You can set the &to parameter to a placeholder which can be filled with one of the fields in your form. Try this:
[!eForm? &to=`[+staffmember+]` &eformOnBeforeMailSent=`getEmailAddress` ..... !]
and change your function to this:
<?php
function getEmailAddress( &$fields ) {
$id = $fields['staffmember'];
// get email address
$result = $modx->db->select('value', 'modx_site_tmplvar_contentvalues', 'contentid = '. $id . ' AND tmplvarid = 49');
while($row = $modx->db->getRow($result)) {
$fields['staffmember'] = $row['value'];
}
return true;
}
?>
This will of course destroy your original ’staffmember’ value. If that bothers you create an extra (hidden) field with a dummy value in your form and use that instead. Also make sure you don’t use PHX with this setup as the placeholder in the snippet call will be destroyed by PHX before it’s of any use to you.