8
I have a page where I check if the user has filled all the fields of a form but when trying to show the messages to the user the script returns me an error, I’m doing it this way:
class SendEmail
{
   private $nome;
   private $email;
   private $telefone;
   private $cidade;
   private $uf;
   private $assunto;
   private $mensagem;
   private $receive_mail;
   public function __construct($data)
   {
    $this->receive_mail = "[email protected]";
      try
      {
        if (count($data) sendMail();
      } catch (Exception $e) {
            return json_encode(array('success'=>'0','errors'=>$e->getMessage()));
      }    
     }
     public function sendMail()
     {
      $subject = $this->assunto;
      $body = "From {$this->nome}, \n\n{nl2br($this->mensagem)}";
      $headers = 'From: ' . $this->email . "\r\n" .
        'Reply-To: ' . $this->email . "\r\n" .
        'Content-type: text/html; charset=utf-8' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
       if (mail($this->receive_mail, $this->assunto, $body, $headers)) {
          return json_encode(array('success'=>'1','message'=>'Mensagem enviada com sucesso!'));
       } else {
          throw new Exception("Ocorreu um erro no envio da mensagem, por favor, tente mais tarde.");
       }
     }
}
if ($_POST) {
    // Aqui ele retornará um JSON com os erros ou a mensagem de sucesso
    echo json_encode(new SendMail($_POST)); 
}
And the whole message is this:
Catchable fatal error: Object of class SendEmail could not be converted to string in /home/anc/public_html/anc/sendEmail.php on line 84
The problem is I’m trying to show a Objeto as string from what I read in the php, but I couldn’t solve.
The email message should be accessible via the property or method of an object and not only by the constructor.
print new SendEmail($_POST);– rray
Could put the builder of this class?
– rray
I just edited the question with the full code.
– adventistapr