• Good Day Folks,

    I have built a validation php script for my form, and I would like to send mail after validation with wp_mail. wp_mail appears to be in pluggable.php. I would like to include pluggable.php in my validator, but it doesn’t seem to work. Here is how my code is laid out:

    I want to do a jquery post call to the following script, which will send my mail or return back an error. Is what I’m doing workable? I can send via mail() but not wp_mail.

    wp-content/themes/mytheme/contactFormValidator.php

    <?php
    	include '../../wp-includes/pluggable.php';
    	header('Content-Type: application/json');
    	/*
    	return codes:
    		0: success
    		1: no name
    		2: no email
    		3: no message
    		4: server error
    	*/
    
    	//sanitizing the post variables
    
    	$returnObj = array();
    	$name = sanitize($_POST['name']);
    	$email = sanitize($_POST['email']);
    	$message = sanitize($_POST['message']);
    	$contact = sanitize($_POST['contact']);
    	$company = sanitize($_POST['company']);
    	//
    
    	if(!isset($name)){
    		$returnObj["msg"] = "no name";
    		$returnObj["code"] = 1;
    		die(json_encode($returnObj));
    	};
    
    	if(!isset($email)){
    		$returnObj["msg"] = "no email";
    		$returnObj["code"] = 2;
    		die(json_encode($returnObj));
    	}; 
    
    	if(!isset($message)){
    		$returnObj["msg"] = "no message";
    		$returnObj["code"] = 3;
    		die(json_encode($returnObj));
    	};
    
    	sendEmail($returnObj, $name, $email, $message, $contact, $company);
    
    	function sanitize($input){
    		$output  = mysql_real_escape_string($input);
    		$output = $input;
    		if(strlen($output)==0){
    			$output = NULL;
    		}
    		return $output;
    	};
    
    	function sendEmail($returnObj, $name, $email, $message, $contact, $company){
    		$returnObj["name"] = $name;
    		$returnObj["email"] = $email;
    		$returnObj["message"] = $message;
    		$returnObj["contact"] = $contact;
    		$returnObj["company"] = $company;
    
    		$to = "ssackler@fourthwall.ca";
    		$subject = "potential sale";
    		$message = "Name: ".$name." Email: ".$email." Message: ".$message;
    		$sendSuccess = wp_mail($to, $subject, $message);
    		if($sendSuccess){
    			$returnObj["msg"] = "success";
    			$returnObj["code"] = "0";
    			die(json_encode($returnObj));
    		}
    
    		die("email not sent");
    	};
    
    ?>

  • The topic ‘can I use wp_mail through an ajax call from a form?’ is closed to new replies.