简体   繁体   中英

How to send email from wordpress php?

I have a problem sending an email from a contact page form in Wordpress.

This is the form:

<?php
  /*
  Template Name: Contact
  */
  ?>
<?php get_header();?>
<div class="container-fluid main-content content-color">
  <div class="row justify-content-center">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 pb-5">
      <!--Form with header-->
      <form action="" method="post">
        <div class="card border-primary rounded-0">
          <div class="card-header p-0">
            <div class="bg-info text-white text-center py-2">
              <h3><i class="fa fa-envelope"></i> Contactanos</h3>
              <p class="m-0">Con gusto te ayudaremos</p>
            </div>
          </div>
          <div class="card-body p-3">
            <!--Body-->
            <div class="form-group">
              <div class="input-group mb-2">
                <div class="input-group-prepend">
                  <div class="input-group-text"><i class="fa fa-user text-info"></i></div>
                </div>
                <input type="text" class="form-control" id="nombre" name="nombre" placeholder="Nombre y Apellido" required>
              </div>
            </div>
            <div class="form-group">
              <div class="input-group mb-2">
                <div class="input-group-prepend">
                  <div class="input-group-text"><i class="fa fa-envelope text-info"></i></div>
                </div>
                <input type="email" class="form-control" id="nombre" name="email" placeholder="ejemplo@gmail.com" required>
              </div>
            </div>
            <div class="form-group">
              <div class="input-group mb-2">
                <div class="input-group-prepend">
                  <div class="input-group-text"><i class="fa fa-comment text-info"></i></div>
                </div>
                <textarea class="form-control" placeholder="Envianos tu Mensaje" required name="msj"></textarea>
              </div>
            </div>
            <div class="text-center">
              <input type="submit"  value="Enviar" class="btn btn-info btn-block rounded-0 py-2">
            </div>
          </div>
        </div>
      </form>
      <!--Form with header-->
    </div>
  </div>
</div>
<?php get_footer(); ?>

Is it possible to send an email to a static email by pressing the send button? I tried using JavaScript, but it doesn't send anything and I don't understand the function that Wordpress has wp_email() since whenever I call it it shows this error:

"Uncaught Error: Call to undefined function wp_mail () "

Your form is not working because there is no action in your form. It should be something like:

<form method="post" action="/send-email.php">

And in send-email.php file you will have your email function. Example:

<?php
if(isset($_POST['submit'] == 'Send')) {
    $ToEmail = 'sample@sample.com';
    $EmailSubject = 'Contact Form';
    $mailheader = "From: " . $_POST["email"] . "\r\n";
    $mailheader .= "Reply-To: " . $_POST["email"] . "\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $MESSAGE_BODY = " <strong>Name:</strong> " . $_POST["name"] . "";
    $MESSAGE_BODY .= "<br> <strong>Email:</strong> " . $_POST["email"] . "";
    $MESSAGE_BODY .= "<br> <strong>Telephone:</strong> " . $_POST["telephone"] . "";
    $MESSAGE_BODY .= "<br><br> <strong>Message:</strong> " . nl2br($_POST["message"]) . "";
    $status = mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die("Failure");

}

if($status){
    echo "your success message";
}
 ?>

The above code is untested but it should work if adjusted to your form fields. You can use the wp_mail() instead of php mail() in the above file.

An alternative way to submit your form is via AJAX. You will have to disable the default behavior of your Submit button and add jQuery (or JavaScript) code that on click it will send your form-data via Ajax. This answer or this one can help you with that.

Lastly, you can always use a popular form plugin (like CF7 ) to save yourself a lot of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM