Passing email address to custom email action from hidden field in form

MaestroC 10 Nov, 2015
This should be an easy one but I have tried a bunch of iterations on this and can't figure out what I am doing wrong. Any help would be greatly appreciated:

I have a survey form that is getting a few hidden fields filled in automatically by a script running in a module on the same page. The script grabs the coach's name and email address and inserts them as hidden fields like this:

<input name="Client[coachnum]" id="coachnum" value="52" type="hidden" class="form-control A">
<input name="Client[coachname]" id="coachname" value="Chad Johnson" type="hidden" class="form-control A">
<input name="Client[coachemail]" id="coachemail" value="criswell@xyz.com" type="hidden" class="form-control A">

These fields are then being used along with TCPDF to produce a PDF output file that is saved to the server then emailed out via a custom code action that looks like this:

<?php 
$mailer = JFactory::getMailer();
$config = JFactory::getConfig();
$sender = array( 
    $config->get( 'mailfrom' ),
    $config->get( 'fromname' ) 
);
$userid = $form->data['coachnum'];
$mailer->setSender($sender);
$user = JFactory::getUser($userid);
$recipient = $form->data['Client[coachemail]'];  
$mailer->addRecipient($recipient);
$mailer->setSubject('GPS Survey Form Response');
$body   = '<h2>Survey Form Response</h2>'
    . '<div>A client has submitted a survey with your Coach ID #.  Please refer to the attached PDF.';
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
$mailer->addAttachment($form->files["cf_pdf_file"]["path"]);
$send = $mailer->Send();
if ( $send !== true ) {
    echo 'Error sending email: ' . $send->__toString();
} else {
    //echo 'Mail sent';
}

?>


The problem is that when a user fills out the form and submits the email errors with this message:
Notice
You must provide at least one recipient email address.
Error sending email: You must provide at least one recipient email address.


I take that to mean that I have the syntax wrong to define the $recipient. Any idea how I need to change that line in order to let it know and use the coach's email address that is being passed by the hidden field in the form?
GreyHead 10 Nov, 2015
Hi MaestroC,

That all looks like hard work.
$recipient = $form->data['Client']['coachemail'];  

Bob
MaestroC 11 Nov, 2015
That was it! Perfect! Thank you!
MaestroC 18 Nov, 2015
I have a followup on this thread. Another hopefully simple yet annoying question...

Using the same code that I did before my email sending custom code action now looks like this:

<?php 
$mailer = JFactory::getMailer();
$config = JFactory::getConfig();
$sender = array( 
    $config->get( 'mailfrom' ),
    $config->get( 'fromname' ) 
);
$userid = $form->data['coachnum'];
$mailer->setSender($sender);
$user = JFactory::getUser($userid);
$recipient = $form->data['Client']['coachemail'];   
$mailer->addRecipient($recipient);
$mailer->setSubject('GPS Survey Form Response');
$body   = '<h2>Survey Form Response</h2>'
    . '<div>A client has submitted a survey with your Coach ID #.  Please refer to the attached PDF.';
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
$mailer->addAttachment($form->files["cf_pdf_file"]["path"]);
$send = $mailer->Send();
if ( $send !== true ) {
    echo 'Error sending email: ' . $send->__toString();
} else {
    //echo 'Mail sent';
}

?>


And it works fine. Except now I want to add the lastName field from the form to the subject line of the email. I have looked through several different forum posts for both CF4 and CF5 (I'm on 5) and have tried to do things like this:

$mailer->setSubject('GPS Survey Form Response from {$form->data['Client']['lastName']}');

But when I put that one it it doesn't even let the email get sent.

$mailer->setSubject('GPS Survey Form Response from {lastName}');

When I use that one it comes to the inbox as "GPS Survey Form Response from {lastName}"

What should I use to get that lastName (and firstName for that matter) into the form's subject line?
GreyHead 19 Nov, 2015
Hi MaestroC,

This should work

$subject = 'GPS Survey Form Response '.$form->data['Client']['lastName'];
$mailer->setSubject($subject);
or if you prefer it in one line
$mailer->setSubject('GPS Survey Form Response '.$form->data['Client']['lastName']);
Note that you can't use the PHP {$variable} syntax inside single quotes - it will work inside double quotes ""

Bob
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger