Sam Skirrow
Sam Skirrow

Reputation: 3697

wordpress php mail send contact form to admin email

I have built a form builder plugin. The last step is to get the emails to send to the site admin, however, I can't seem to figure out how to make this work. Here is my php mail handler:

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 = get_option('admin_email');
$subject = "Form Submission";
$txt = $html;
$headers = "From: <[email protected]>". "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1"."\r\n";
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);

You can see in my $to variable I have added get_option('admin_email'); however, doing this breaks my form (as opposed to just writing an email address in.

Upvotes: 0

Views: 2579

Answers (1)

Luka Peharda
Luka Peharda

Reputation: 1013

You need to load WordPress to be able to use their get_option function.

Add this to the top of your PHP script:

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

Upvotes: 1

Related Questions