简体   繁体   中英

How to use php mail() function

I know this code will give error on local host and not working on live server.

As you can see i am trying to run this code for mail function. this function is running on my other site but working in this site. know this code will give error on local host but not showing error. may be some issue with the form. i do not know what exactly the error please tell me the issue.

function send-message()
{
      if(ISSET($_POST['submit'])) {
        $to        = "abc@example.com";
        $FROM_NAME= $_POST['name'];
        $cell = $_POST['cell'];
        $email     = $_POST['email'];
        $subject   = $_POST['subject'];
        $message   = $_POST['message'];
    
        $headers = "From:{$FROM_NAME} {$email} {$cell}";
        $result = mail($to, $subject, $message, $headers);
        if (!$result) {
          echo "error";
        }
        else{
          echo "sent";
        }
      }
  }

here is a form please address the error

<form method="post" id="contact-form" action="">
   <?PHP send-message(); ?> // function call here
   <div class="input-box mt-25">
       <label for="#">Full Name Here</label>
       <input type="text" name="name" placeholder=" Enter Name Here" required="">
       <i class="FAL FA-user"></i> //please ignore capital letter
    </div>
   <div class="input-box mt-25">
       <label for="#">Phone Number</label>
       <input type="text" placeholder="Enter your phone number *" name="cell" required="" max length="11" min length="11">
        <i class="FAS FA-phone-square"></i>
   </div>
   <div class="input-box mt-25">
       <label for="#">I Would Like To Discuss</label>
       <input type="email" name="email" placeholder="Enter your email" required="">
         <i class="far FA-envelope"></i>
   </div>
     <div class="input-box mt-25">
       <label for="#">Subject</label>
       <input name="subject" type="text" placeholder="Subject *" required="">
       <i class="FAS FA-book"></i>
   </div>
   <div class="input-box mt-25">
       <label for="#">Leave A Message</label>
       <text area name="message" id="#" cols="30" rows="10" placeholder="Write Message"></text area>
       <i class="FAL FA-pen-alt"></i>
   </div>
   <div class="submit-BTN text-left mt-20">
       <button type="submit" name="submit" class="main-BTN">Send Message</button>
   </div>

There is multiple errors in your code.

send-message does not seems to be a valid function name.

An other problem is about the headers part, check the official php.net/mail documentation to see how to use the mail function properly, and how to write proper headers, and when to go on new line.

There is probably more errors, I suggest you to enable display_errors and error_reporting when you're developping (again, check official documentation to see how to enable it if you don't know:) )

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