0

I plan to create a contact form. The validation errors are operating at 100% but when there is no error when sending can not receive email and do not know why. I've tried several ways someone can help me. The code is this:

CONTACT:

<?php 

require_once('../classes/db_funcoes.php');
require_once("../classes/contacto_class.php");

?> 

                <form method="post"> 
                    <p style="padding: 0 20px; margin-top: -15px;"><span style="color: red;">
                        <?php 

                        if(isset($_POST['submit'])) {
                            $sendMail = new contactos();
                            $sendMail->nome   = $_POST['nome'];
                            $sendMail->email  = $_POST['email'];
                            $sendMail->assunto  = $_POST['assunto'];
                            $sendMail->mensagem  = $_POST['mensagem'];
                            $sendMail->sendMail();
                        } ?>
                    </span></p>
                            <input name="nome" type="text" class="form-control" id="name" placeholder="ex: João" value="<?php if (isset($_POST['nome'])) { echo $_POST['nome']; } ?>"/>
                            <input name="email" type="email" class="form-control" id="email" placeholder="ex: email@sapo.pt" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" />
                            <select id="subject" name="assunto" class="form-control" >
                                <option value="na" selected="">Escolher Assunto:</option>
                                <option value="service">Problemas de serviço</option>
                                <option value="suggestions">Sugestões</option>
                                <option value="product">Produtos</option>
                            </select>
                            <textarea style="resize: none; color: #222;" name="mensagem" id="message" class="form-control" rows="9" cols="25" placeholder="Mensagem"><?php if (isset($_POST['mensagem'])) { echo $_POST['mensagem']; } ?></textarea>
                        <input type="submit" name="submit" class="btn btn-primary pull-right" id="btnContactUs" value="Enviar Mensagem">
                </form>

CONTACT_CLASS:

<?php

class contactos {

    var $nome;
    var $email;
    var $assunto;
    var $mensagem;
    var $admin;

    public function sendMail() 
    {
            $errors = "";
            $admin = "josecerejo@sapo.pt"; 

            if ($this->nome == "") {
                $errors .= '- You forgot to enter a name!<br />';
            }

                if ($this->email == "") {
                $errors .= '- You forgot to enter an email!<br />';
            }

                if ($this->assunto == "") {
                $errors .= '- You forgot to enter a assunto!<br />';
            }

                if ($this->mensagem == "") {
                $errors .= '- You forgot to enter a mensagem!<br />';
            }

            if(empty($errors)) {
            $headers = "From: ".$this->nome." <".$this->email.">";
            $send_contact=mail("$this->admin","$this->assunto","$this->mensagem","$headers");
            exit();
            } else {
                echo '<p class=\'message error\'>';
                echo '<font color="black">' . $errors . '</font>';
                echo '</p><br />';
            }
        } 
    } 
?>
Jose Cerejo
  • 231
  • 1
  • 5
  • 19
  • What does `var_dump($send_contact);` say before `exit();`. Also what OS is the this script running on (e.g. Linux or Windows)? – dbf May 13 '15 at 21:48
  • this script running in windows. i change for this: "$send_contact=mail("$this->admin = 'josecerejo@sapo.pt'","$this->assunto","$this->mensagem","$headers");" and nothing – Jose Cerejo May 14 '15 at 01:14

1 Answers1

0

$this->admin is null because you're setting $admin which is only available inside the sendMail() function's scope.

Either set $this->admin or use $admin in the mail() function:

$this->admin = 'your@email.com';
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • I changed as you have said and did not work in the same if(empty($errors)) { $headers = "From: ".$this->nome." <".$this->email.">"; $send_contact = mail("$this->admin = 'josecerejo@sapo.pt'","$this->assunto","$this->mensagem","$headers"); } else { echo '

    '; echo '' . $errors . ''; echo '


    '; }
    – Jose Cerejo May 14 '15 at 01:19