-1

i have created this php form for enquiry purpose but when i clicked on the submit button it's not working. can you tell me what have i done wrong in this code.

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type ="text" name="subject" placeholder="subject">
    <input type ="email" name="email" placeholder="email">
    <textarea rows="5" name="message" cols="30"></textarea>
    <input  class="btn" type="submit" name="submit" value="Submit">
</form>


   <?php
if(isset($_POST['submit'])){
    $to = "rizwandesigns19@gmail.com";
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $from = $_POST['email'];
    
    $headers = "From : $from";

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

echo "Mail Sent";
?>

or can you give me valid php script for this purpose.

Rizwan
  • 9
  • 2
  • 1
    Please update your question and be specific about what you're asking. Also format your codes correctly. – Siraj Alam Feb 12 '21 at 07:10
  • Use the following: [How to send an email using PHP?](https://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php) and [Get all variables sent with POST?](https://stackoverflow.com/questions/8207488/get-all-variables-sent-with-post) – Definitely not Rafal Feb 12 '21 at 07:12
  • now you can check i have formated the code and also specify the problem – Rizwan Feb 12 '21 at 07:40
  • Does `mail($to, $subject, $message, $headers);` return `true`? There might be multiple reason why your email is not being send: [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Definitely not Rafal Feb 12 '21 at 07:46

1 Answers1

1

To get through google's and other's spam filters you need an address which the mail is sent from. For this you could create a new Gmail-address and use the phpmailer-library.

Edit: Setup with gmail is actually pretty complicated and it will disable your access once in a while which renders it almost unuseable. I started to do it by installing a real mail server on the host machine (Install a Complete Mail Server with Postfix and Webmail in Debian 9) and use the php mail() function (phpmailer can still be used tho).

DrMaxNix
  • 73
  • 5
  • One reflection: Would you still get through the spam filters if PHPMailer sends the email in the servers "name"? – Dubstepzedd Feb 07 '22 at 23:41
  • Dubstepzedd, yes that's also possible and I actually got that to work better than using gmail because google will disable your app-token every 2 weeks or so.. it's nearly unuseable.. Sending mail from localhost requires you to set up a mail server on this server (eg postfix). For the mail not to be marked as spam you will also have to add some dns-records to your domain (mx, spf, dmarc, etc) and your server's ip-address should also have a valid reverse-dns (RDNS). – DrMaxNix Feb 10 '22 at 18:28