A Link Thats sends me a automated message

Is there a way where if someone pushes a link it would send me an automated message? This is the only code I found. Where would i add this code?

<form action="" method="post">
    <input type="submit" value="Send details to embassy" />
    <input type="hidden" name="button_pressed" value="1" />
</form>

<?php

if(isset($_POST['button_pressed']))
{
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "rn" .
        'Reply-To: webmaster@example.com' . "rn" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    echo 'Email Sent.';
}

?>

Related posts

1 comment

  1. PHP is executed only on the server side, you will need some Javascript to get the message to the server to make the mail send that is the first part of the code below. The second part is the PHP that will handle the sending.

    Add this code to your functions.php file and place the HTML wherever you want the button.

    If you need more this is basically a copy of the code here: https://codex.wordpress.org/AJAX_in_Plugins

    <?php
    add_action( 'wp_footer', 'my_action_javascript' ); // Write our JS below here
    
    function my_action_javascript() { ?>
        <script type="text/javascript" >
        jQuery('#id_of_send_button').on("click", function($) {
    
            var data = {
                'action': 'my_action',
                'button_pressed': 1
            };
    
            // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
            $.post(ajaxurl, data, function(response) {
                alert('Got this from the server: ' + response);
            });
        });
        </script> <?php
    }
    
    add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
    
    function my_action_callback() {
        if(isset($_POST['button_pressed'])) {
            $to      = 'nobody@example.com';
            $subject = 'the subject';
            $message = 'hello';
            $headers = 'From: webmaster@example.com' . "rn" .
                'Reply-To: webmaster@example.com' . "rn" .
                'X-Mailer: PHP/' . phpversion();
    
            mail($to, $subject, $message, $headers);
    
            echo 'Email Sent.';
        }
        wp_die(); // this is required to terminate immediately and return a proper response
    }
    

Comments are closed.