-1

I need to send e-mail using data from the form (mailto doesn`t work)

Somehow, php file doesn't connect to html. When I run index.html locally, if I push submit, the browser shows me a php code and doesn't do anything else. When I use localhost, it throws a 405 Error if I push submit.

PLS help since i am not php dev.

<form method="POST" name="myName" action="registration.php">
  <textarea name="message"></textarea>
  <div class="form_submit">
    <input type="submit" value="Submit" class="form_submit-btn">
   </div>
</form>


<?php

if($_POST["message"]) {

mail("myemail@gmail.com", "Here is the subject line",

$_POST["insert your message here"]. "From: an@email.address");

}

?>
xGeo
  • 2,149
  • 2
  • 18
  • 39
Letter
  • 1
  • 1
    Which server are you using? – Jerry Jul 28 '20 at 07:35
  • As Navan asked as well as index.html should be index.php since the code is within the same file (based off your example above). what you really want to do is move the php code to a file called registration.php. and make sure you are running Nginx/Apache with php and have the server configured to read the directory of your application. – Leo Fisher Jul 28 '20 at 07:40

2 Answers2

0

Your call to the mail function does not look quite right - the headers element of the call appears to be appended to the body of the message rather than as a separate parameter.

<?php
    # registration.php
    
    error_reporting( E_ALL );
    ini_set( 'display_errors', 1 );

    
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['message'] ) ){
        ob_clean();
        
        $lb="\r\n";
        
        $recipient='geronimo@example.com';
        $message=$_POST['message'];
        $subject='This is a test';
        $headers=array(
            'From: webmaster@example.com',
            'Reply-To: webmaster@example.com',
            'Cc: joe.bloggs@example.com'
        );
        
        $status=mail( $recipient, $subject, $message, implode( $lb, $headers ) ;
        exit( header( sprintf('Location: index.html?mailsent=%s', $status ) ) );
    }
?>





<!-- index.html -->
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
    </head>
    <body>
        <form method='POST' action='registration.php'>
            <textarea name='message'></textarea>
            <div class='form_submit'>
                <input type='submit' value='Submit' class='form_submit-btn'>
            </div>
        </form>
    </body>
</html>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
-1

Rename your file index.html to index.php. Its throwing 405 Error because index.html cannot accept a POST request. You need to process your form with a server-side language in that case change your index.html to index.php.