-1

I'm learning PHP and working on some basic functions. I was wondering why my send message function doesn't work.

I have 2 PHP files:

  1. One to process the login and
  2. One to process the message.

Any advice?

enter image description here

index.php

<form name="admin" method="POST" action="process.php">
    <div>
        <label for="login">Login: </label>
        <input type="text" name="login" placeholder="Login"/>
    </div>
    <div>
        <label for="password">Password: </label>
        <input type="text" name="password" placeholder="Password"/>
    </div>
    <div>
        <input type="submit" name="submit" value="Submit">
    </div>
</form>
<br>

<form name="sendmail" method="POST" action="sendmail.php">
    <div>
        <p><b>Send me a message:</b></p>
        <textarea name="message"></textarea>
    </div>
    <div><input type="submit" name="submit" value="Send"></div>
    </div>
    
    
</form>

sendmail.php

<?php
$message = '';
foreach($_POST as $name=>$value){
    if('submit'!=$name){
        if(is_array($value)){
            /*  SYNTAX: implode(separator,array) - 
                converts an array of values as a string. */
            $value=implode(', ', $value);
        }
        $message .= ucfirst($name) ."is $value.\n\n";
    }
}
$to      = 'testemail@test.org';
$subject = 'Form submission from webapp';

if (mail($to, $subject, $message)){
    echo "Your message has been sent.";
 };
?>
Azhar Khan
  • 3,829
  • 11
  • 26
  • 32
garu525
  • 11
  • `mail()` doesn't just work -- it has some non-trivial system requirements, namely, that you have a working mail server. If you're just trying to debug, don't use `mail()`, instead output to a log file. If you actually need to send mail, I'd use something like [PHPMailer](https://github.com/PHPMailer/PHPMailer) instead -- but note, you'll _still_ need a mail service to send through. – Alex Howansky Nov 26 '22 at 21:34
  • 1
    Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – ADyson Nov 26 '22 at 21:34
  • Not a fix but please note that the [](https://html.spec.whatwg.org/dev/embedded-content.html#the-input-element) tag does not use and does not need a closing slash and never has in any HTML specification. – Rob Nov 27 '22 at 18:06

1 Answers1

0

From the PHP manua:

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

https://www.php.net/manual/function.mail.php

Dr. Vortex
  • 505
  • 1
  • 3
  • 16