PHP send email to address entered by user in a plugin file

I am creating a form builder plugin for wordpress. Within the plugin settings, the user can set the recipient email address – the code for this is in file class-form-builder.php.

I have a file called class-send-form.php that is a php mail handler. I am trying to add the email address that is entered by the user to this file but returning an error.

Read More

In class-form-builder.php I have the following code:

class Layers_Form_Builder_Widget extends Layers_Widget {

    ...

    function send_to_email() {
        return 'sam@skizzar.com';
    }

    ...

}

At the minute I am using a hardcoded email address to try and get that working before I grab the value inputted by the user.

Then in class-send-form.php I have the following code:

require_once('../../../../wp-load.php');

$layers_email_address = Layers_Form_Builder_Widget::send_email_to();

// if there are any errors in our errors array, return a success boolean of false
if (!empty($errors)) {
// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors'] = $errors;

} else {
parse_str($_REQUEST['formData'], $formFields);
$html='<html><body>';
foreach ($formFields as $key => $value) {
    $html .= '<p><label>' . $key . ' :</label> ' . $value . '</p>';
}
$html .='</body></html>';
$to = $layers_email_address;
$subject = "Form Submission";
$txt = $html;
$headers = "From: <".$to.">". "rn";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/html; charset=ISO-8859-1"."rn";
mail($to,$subject,$txt,$headers);

// if there are no errors process our form, then return a message

// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}

// return all our data to an AJAX call
echo json_encode($data);

However, when I click send, I just get an error (“there was an error when submitting your form”).

How can I get the value of the email address from class-form-builder.php and use it in class-send-form.php?

Related posts

1 comment

  1. The function name used in class-send-form.php is send_email_to() and the function name defined in class-form-builder.php is send_to_email().

    I think once you fix that, it will work.

Comments are closed.