Here is a functional example of sending E-mail with php via POST ..
in this case I did send the information via Ajax .. but you can feel free to implement the front end any way you want! I also used returns in Json to display the error and success msgs on the front!
Good luck!
<?php
header("Content-type: text/html; charset=utf-8");
$filter_rules = [
'name' => FILTER_SANITIZE_STRING,
'email' => FILTER_VALIDATE_EMAIL,
'phone' => FILTER_SANITIZE_STRING,
'site' => FILTER_SANITIZE_STRING
];
$validation = [
'name' =>[
'is_null' => 'Preencha o campo <b>nome</b>!',
'is_false' => 'O Campo <b>nome</b> não é valido!'
],
'email' =>[
'is_null' => 'Preencha o campo <b>e-mail</b>!',
'is_false' => 'O Campo <b>e-mail</b> não é valido!'
],
'phone' =>[
'is_null' => 'Preencha o campo <b>Telefone</b>!',
'is_false' => 'O Campo <b>Telefone</b> não é valido!'
]
];
$data = filter_input_array(INPUT_POST, $filter_rules);
foreach ($data as $field => $value){
if(empty($validation[$field])){
continue;
}
if($value == null or $value == ''){
echo json_encode(["status"=>false,"msg"=> $validation[$field]['is_null'] ]);exit;
} elseif($value === false){
echo json_encode(["status"=>false,"msg"=> $validation[$field]['is_false'] ]);exit;
}
}
$email_sender = '[email protected]';
$subject = 'Subject';
// Message
$message = '
<html>
<head>
<title>Solicitação do cliente</title>
</head>
<body>
<p><b>Nome:</b>'.$data['name'] .'</p>
<p><b>E-mail:</b>'.$data['email'] .'</p>
<p><b>Telefone:</b>'.$data['phone'] .'</p>
<p><b>Site:</b>'.$data['site'] .'</p>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.1' ."\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' ."\r\n";
// Additional headers
$headers .= 'From: '. $email_sender ."\r\n";
$headers .= 'Return-Path: '. $email_sender ."\r\n";
$headers .= 'Reply-To: '. $data['email'] ."\r\n";
// Mail it
if(mail('[email protected]', $subject, $message, $headers, "-f$email_sender")){
echo json_encode(["status"=>true, "msg"=>"Mensagem enviada com <b>sucesso</b>!"]);exit;
}else {
echo json_encode(["status"=>false, "msg"=>"Desculpe ocorreu um <b>erro</b>!"]);exit;
}