0

I have to send an email using PHP with information given in a form on HTML. The problem is that the code i have, doesn´t send the email, this is the code i have so far, is in a .php file:

<html>

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Contact us</title>
</head>

<?php
    $error = false;
    $sent = false;

    if(isset($_POST['submit'])) {
        if(empty($_POST['email']) || empty($_POST['subject']) || empty($_POST['content'])) {
            $error = true;
        }
        else {
            $to = "rae.alejandro@gmail.com";
            
            $email = trim($_POST['email']);
            $subject = trim($_POST['subject']);
            $content = trim($_POST['content']);
            $headers = "From:" . $email;
            mail($to, $subject, $message, $headers);
          
            if($mailsent) {
                $sent = true;
            }
        }
    }
?>


<body>
   <div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="well well-sm">
                <form class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
                    <fieldset>
                        <div class="form-group">
                            <span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
                            <div class="col-md-8">
                                <input id="email" name="email" type="text" placeholder="Correo" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-user bigicon"></i></span>
                            <div class="col-md-8">
                                <input id="subject" name="subject" type="text" placeholder="Asunto" class="form-control">
                            </div>
                        </div>

                        <div class="form-group">
                            <span class="col-md-1 col-md-offset-2 text-center"><i class="fa fa-pencil-square-o bigicon"></i></span>
                            <div class="col-md-8">
                                <textarea class="form-control" id="content" name="content" placeholder="Escribe tu correo aqui, nosotros nos pondremos en contacto" rows="7"></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-12 text-center">
                                <button id="submit" type="submit" class="btn btn-primary btn-lg" onclick="enviar()">Enviar correo</button>
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</div>





<script type="text/javascript">
function enviar(){
    if(ValidarCorreo(document.getElementById('email').value)&&obligatorio(document.getElementById('subject')&&obligatorio(document.getElementById('content')))){
        alert('llenado correctamente');
    }else{
        alert('Falta informacion por llenar')
    }
}

function obligatorio(contenido) {
            var ok = true;
            if (contenido.length == 0) {
                ok = false;
            } else {
                return ok;
            }
     }

function ValidarCorreo(correo) {
            var ok = true;
            var expReg = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
            var esValido = expReg.test(correo);
            if (esValido == true) {
                ok = true;
                return ok;
            } else {
                alert('El coreo no es valido');
                ok = false;
                return ok;
            }
    }
</script>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
        integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
        integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
        crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
        crossorigin="anonymous"></script>
   
</body>


</html>

I don´t know if i should put the php code on another file or put it just in the same file, also i have this code running on a free hosting service, awardspace, so i don´t know if this is the reason i´m not able to send emails becasue in the free service, o don´t have access to email accounts.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • 1
    [RTM](https://www.awardspace.com/kb/php-mail-function/#:~:text=A%20word%20of%20warning%3A%20AwardSpace,option%20to%20get%20a%20refund.) – mplungjan Apr 26 '21 at 07:36
  • Also NEVER call anything in a form `id="submit"` or `name="submit"` if you ever plan to submit the form using script – mplungjan Apr 26 '21 at 07:37
  • 1
    Where do you define $mailsent? You could echo any error message back to the client – mplungjan Apr 26 '21 at 07:38

0 Answers0