Search code examples
ajaxwordpressemailsend

How can I create a custom email sender file in Wordpress?


I created a custom contact form in JavaScript which collects data in JSON.
I would like to pass it via HTTP POST to a PHP file which should format the email and just send it, but I can't find the file via the Ajax call because I get a 404 error.

Is there a correct way to do this?

If I put it in the root directory I can access it, but it does not receive POST data.

This is how I'm trying to pass data to my custom file which now is in: /wp-content/themes/themeName/customMailer.php

Angular 1 -

$http.post(
    '<?php echo get_site_url();?>/customMailer.php',
    $scope.FormData
).then(
    function(e){
        console.log(e);
    }, 
    function(){
       console.log("error");
    }
);

Solution

  • I resolved moving my PHP code inside the functions.php file of Wordpress, and creating a function which had inside my code that sends the email.

    Then I created two hooks to my php function with add_action() (method of wordpress).

    In my Angular code I added on submit an Ajax call that makes a POST to admin-ajax.php, with a variable called "action" that has a value representing the name of my php funcion before created in functions.php.