TransWikia.com

Send an email to specific adress when button is clicked?

WordPress Development Asked by joq3 on December 3, 2020

I have created a custom page template with a search function, this search function shows posts, and I want to create a button on each post that when it is clicked will send an email to the author of the post the_author_email();

I am trying to figure out how to do this. I want to be able to have the email sent without the page reloading, is this possible?

Thank you!

One Answer

You need to call a JavaScript function when the button is clicked. It should be stored directly in the .php template and it should look like this:

function SubmitData() {
    var email = <?php echo get_the_author_meta('email'); ?>;
    $.post("submit.php", { email: email }
}

Then you need to create a PHP script, it could be stored in submit.php file (as seen above in your JavaScript):

    <?php
      $from = "[email protected]"; 
      $to = $_POST['email']; // this is the author's email

      $subject = "Your subject"; 
      $message = "Your message (it can contain HTML as well)";

      $headers  = "MIME-Version: 1.0rn";
      $headers .= "Content-type: text/html; charset=UTF-8rn";
      $headers .= "From:" . $from;

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

You can pass other variables (e.g. subject or the message contents) the same way as the email address.

I hope this helps, let me know if it worked for you.

Answered by Kristián Filo on December 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP