Problems sending mail with mail()

Asked

Viewed 107 times

0

I am trying to send the email, however is returned the success message, but the message is not sent.

I send the data from the frontend to the backend with Angular and the data arrives straight, 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;
  }

Accommodation is the Hostinger. I don’t know if it has anything to do with.

  • Welcome to Stackoverflow in English. I edited your question to remove the greetings, as we usually keep them as clean as possible to focus on your scheduling question. If you are interested in visiting a part of the site that is not aimed to ask questions can know the Stack Overflow Chat in Portuguese. If you have questions about the operation, rules and procedures of the site visit the Stack Overflow in English Meta :)

  • The code uses the client’s e-mail as the sender (From), this could be a problem for the DNS Reverso, ie, basically the recipient checks if the sender IP is the same IP as the sending server and then considers the message as spam if the IP is not the same.

2 answers

0

I believe it’s the fact that you’re skipping a parameter:

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

Check out 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.';
  • Thiago, you say the parameter $message? $enviar email = mail($destination, $subject, $message, $headers);? Pq tbm did so, and n is going n.

  • I’ll do the following I’ll put a tested and working PHP code here for you, then you just change according to your need, beauty?!

  • Damn Thiago, mt obgd for posting the code. I think what’s happening is that the friend said above, because I’m testing with my email from Hotmail, tlvz only works with email from the client domain.

  • Yeah, I use Hostgator, in Hostinger there may be some limitation, but you can do exactly what the friend commented, I’ll update the code so it has this functionality too!

0

One detail is that the function mail() 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 true.

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

Until why email in most cases ends up going to a queue, which makes your email not be sent immediately, but it will be in the near future if you think about it the return would be false which would lead to various complications.

In some accommodations the header from has to be a valid and existing email from your domain, otherwise do not send the email, then add the reply-to to the email that was on from. Something like that.

$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";
  • Vixe!! Then I’ll have to pick up the email from the client’s domain, because it’s being sent to me from Hotmail. It was worth help bro, I ask him, and put the result here.

  • There is the problem of DNS Reverso, ie, basically the recipient checks if the sender IP is the same IP as the sending server and then considers the message as spam if the IP is not the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.