1
I am developing a form template within a wordpress theme. I need the form data to be sent via e-mail to a certain address via AJAX. Here is the processing of the form in JS:
var FormData = function(){
var user_name;
var user_email;
var user_message;
this.setName = function(name){
this.user_name = name;
}
this.getName = function(){
return this.user_name;
}
this.setEmail = function(email){
this.user_email = email;
}
this.getEmail = function(){
return this.user_email;
}
this.setMessage = function(message){
this.user_message = message;
}
this.getMessage = function(){
return this.user_message;
}
this.nameIsValid = function(){
if(this.user_name != ''){
return true;
} else {
return false;
}
}
this.emailIsValid = function(){
if(this.user_email != ''){
return true;
} else {
return false;
}
}
this.messageIsValid = function(){
if(this.user_message != ''){
return true;
} else {
return false;
}
}
this.formIsValid = function(){
if((this.nameIsValid() && this.emailIsValid() && this.messageIsValid())){
return true;
} else {
return false;
}
}
};
$(document).ready(function(){
user_object = new FormData();
});
Some may find it unnecessary to validate a form. But I did it only for study purposes anyway. Omitting the methods I believe to make no difference, I proceed.
$('#form-contact').submit(function(e){
e.preventDefault();
user_object.setName($('input[name=user_name]').val());
user_object.setEmail($('input[name=user_email]').val());
user_object.setMessage($('#user_message_textarea').val());
if(user_object.formIsValid()){
var formData = JSON.stringify(user_object);
$.ajax({
type: 'POST',
dataType: 'json',
cache: false,
url: '/wp-admin/admin-ajax.php',
data: formData,
success: function(data){
alert(data);
$('#form-contact').trigger('reset');
},
error: function(){
alert("Deu erro aqui!");
}
});
} else {
alert("Este formulário não é válido!");
}
I want to send the object in AJAX notation for the PHP method. Now the server-side code:
function sendMail(){
$obj = json_decode($_POST['formData']);
$user_name = $obj->{'user_name'};
$user_email = $obj->{'user_email'};
$user_message = $obj->{'user_message'};
$send = mail('[email protected]', 'Contato pelo Site', $user_message);
if ($send) {
return json_encode("Certinho!");
} else {
return json_encode("Deu erro!");
}
}
add_action('wp_ajax_sendMail', 'sendMail');
add_action('wp_ajax_nopriv_sendMail', 'sendMail');
Now the problem! When I submit the form, I debug the AJAX request, and it is giving status 200. In fact, I receive the success message as a response. However, the PHP function is not really executed and I do not receive any email.
Where I went wrong?
Where do you call the function
sendMail()
in PHP?– Sergio
I don’t actually call the function in PHP. sendmail is a PHP function that just decodes the json object and uses it as an argument for the mail function. In this case, this json object is passed at the time of the request.
– Pedro Vinícius
@Sergio, in theory, the
sendMail
should be defined within of the objectformData
, but this is not in the code displayed... . . . . Pedro, which "function is not actually executed"? Themail()
PHP? It is not clear which error you are talking about, because it showed a lot of code to end up talking about something else (?).– brasofilo
Yes @brasofilo. The mail() function that does not execute. The Success function in javascript is executed. The data is being sent to php’s function as json object, but for some reason, the function in php is not running or something is wrong.
– Pedro Vinícius
Try with
wp_mail
and activating the SMTP– brasofilo
@brasofilo, I’ve tried this too :-/ SMTP enabled. My code is correct?
– Pedro Vinícius
As I said, you speak of Ajax, form, JS, Wordpress, but in the end your problem is PHP Mail :/ If you want to solve this problem, please edit the question to reflect this (citing that you have already tried wp_mail and smtp). . . . . I would make some security adjustments and use of native WP functions, but if the code works and the Ajax request returns correctly, then yes, the code is ok. Your problem is elsewhere...
– brasofilo
@Brasofilo, they’re all related matters. I just described the problem with its details, because I don’t know where my mistake is and I thought that would be easier to understand. But that’s okay.
– Pedro Vinícius
It’s just that I don’t think it’s about one thing and another, and you’re concentrating on technical details where there’s nothing wrong... Check the results in the OS for
[php] debug mail
.– brasofilo