1

i have a website which is built in HTML and PHP . now to send mail I have made a contact form which looks like the following:

<form class="" method="post" dat-email="" action="">
  <input type="text" class="form-control" name="name" placeholder="Name" required>
  <input type="text" class="form-control" name="email" placeholder="Email" required>
  <textarea class="form-control" name="message" placeholder="Your Message" required></textarea>
  <input name="submit" class="def-btn" type="submit" value="submit">
</form>

for the form to work I have added the following PHP mail function:

<?php
if(isset($_POST['submit'])){

   $headers .= "Return-Path: The Sender <zubairnazer@gmail.com>\r\n";
   $headers .= "From: The Sender <zubairnazer@gmail.com>\r\n";
   $headers .= "Organization: Sender Organization\r\n";
   $headers .= "MIME-Version: 1.0\r\n";
   $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
   $headers .= "X-Priority: 3\r\n";
   $headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;

    $to = "zubairnaze@gmail.com";
    $from = $_POST['email'];
    $first_name = $_POST['name'];
    $last_name = $_POST['message'];

    $subject = "The Nosh Bistro Contact";

    $message = $first_name . " has sent the following message.  " . "\n\n" . $last_name. "\n\n"."Email " .$from. "\n\n" ;

  
    mail($to,$subject,$message,$headers);
   echo "<script type='text/javascript'>alert('Message Sent Successfully!')</script>";

        }
?>

before it was working fine when I used only one header, but now I added these many headers so that my mail doesn't goes to spam folder. but now the mail function is not sending mails to my gmail id and I am getting the following error:

Notice: Undefined variable: headers in /hermes/walnacweb05/walnacweb05aa/b185/as.moluguz/nosh/index.php on line 1785

can anyone please tell me what could be wrong here

  • You may find that your mail server isn't happy to send emails from the address that your form user fills in - most mail servers only send from a domain that they're configured for. If you're using gmail to _send_ the email as well, you need to enable SSL and select the appropriate port. You should consider using PHPMailer instead of the built-in `mail()` function, too. – droopsnoot Nov 04 '20 at 08:39
  • Why is your message contained in a variable called `$last_name`? That seems confusing, to me. Why not call it `$message`? – droopsnoot Nov 04 '20 at 08:40
  • Your success message appears whether or not the `mail()` function worked or didn't work - you don't check for the return from that function, and just display success anyway. Whether you stick with `mail()` or use a different library, you should check to see what happened before deciding it all worked. – droopsnoot Nov 04 '20 at 08:41
  • Which of these specific headers if preventing the email from sending? If you remove them one by one, at what point does it start sending again? – droopsnoot Nov 04 '20 at 08:43
  • @droopsnoot ssl is enabled bro, i gave my alternate email In the form box – Zubair Nazer Oliyat Nov 04 '20 at 08:45
  • @droopsnoot i have updated the question with the error I am getting now bro – Zubair Nazer Oliyat Nov 04 '20 at 08:50
  • seems to me like you are concatenating $headers variable but you have not initialised it anywhere. so you can change the first line to `$headers = "Return-Path: The Sender \r\n";` – Rishabh Kalakoti Nov 04 '20 at 09:12

0 Answers0