Problems sending mail with mail () function

0

I'm trying to send an email, but the success message is returned, but the message is not sent.

I send the data from the frontend to the backend with the Angular and the data arrives right, but I do not know what happens that does not send.

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");

$_POST = json_decode(file_get_contents('php://input'), true);

$name      = $_POST['name'];
$email     = $_POST['email'];
$phone     = $_POST['phone'];
$place     = $_POST['place'];
$message   = $_POST['message'];


  $destino = "[email protected]";

  $assunto = "Cliente.";

  $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      $headers .= 'From: $name <$email> '. "\r\n";
      $headers .= 'Telefone: $phone '. "\r\n";
  //....

  $enviaremail = mail($destino, $assunto, $headers);

  if($enviaremail)
  {
  echo 1;
  } 
  else 
  {
  echo 0;
  }

The host is the hostinger. I do not know if there's anything to see.

    
asked by anonymous 06.02.2017 / 12:17

2 answers

0

One detail is that the mail() function returns a boolean for the execution of the function so to speak, and not if the email was actually sent. The function was executed correctly, so it comes with true .

It is not PHP that sends the email, but some other software installed on the server, so PHP is not responsible for informing if the email actually left your server (sent).

Why does email in most cases end up in a queue, which causes your email not to be sent immediately, but it will be in the near future, if you think on that side the return would be false what would it take to various complications.

In some lodgings, header from has to be an existing valid email from your domain, otherwise it does not send the email, then it adds reply-to to the email that was in from . More or less.

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Reply-to: $name <$email> '. "\r\n";
$headers .= 'From: [email protected] '. "\r\n";
$headers .= 'Telefone: $phone '. "\r\n";
    
06.02.2017 / 12:52
0

I think it's the fact that you're jumping a parameter:

  

bool mail (string $ to, string $ subject, string $ message [ string   $ additional_headers [ string $ additional_parameters]])

Please check the PHP Manual .

I use the code as follows (without those headers you use, try removing them):

$name      = $_POST['name'];
$email     = $_POST['email']; 
$phone     = $_POST['phone'];
$place     = $_POST['place'];
$message   = $_POST['message'];

$m['subject'] = 'Qualquer Assunto'; 
$m['to'] = '[email protected]';

$m['message'] = '
E mail de contato:<br /><br />

Nome: '.$name.'<br />
E-mail: '.$email.'<br />
Telefone: '.$phone.'<br />
Localização: '.$place.'<br />
Mensagem: '.$message.'<br />

';

$m['headers'] = implode("\r\n",array(
                        'From: Contato <[email protected]>',
                        'Reply-To: Contato <[email protected]>',
                        'Subject: '.$m['subject'],
                        'Mime-Version: 1.0',
                        'Content-Type: text/html; charset=iso-8859-1',
                        'Priority: normal',
                        'X-Mailer: PHP/'.phpversion(),
                        'X-Priority: 3'));;

if(mail($m['to'],$m['subject'],$m['message'],$m['headers'])){   
    echo 'Seu contato foi enviado com sucesso.'; 
}
else echo 'Seu contato falhou.';
    
06.02.2017 / 12:31