When sending an email using the php mailer class, I receive two emails and duplicates: one contains the information without files while the other shows me the information plus the corresponding files
$to_Email = "[email protected]"; //Replace with recipient email address
$subject = 'Contacto Sitio Web'; //Subject line for emails
$emailPagina = "[email protected]";
//check $_POST vars are set, exit if any missing
if(!isset($_POST["nombre"]) || !isset($_POST["rut"]) || !isset($_POST["email"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Asegurece de llenar los campos correctamente'));
die($output);
}
//Sanitize input data using PHP filter_var().
$nombre = filter_var($_POST["nombre"], FILTER_SANITIZE_STRING);
$rut = filter_var($_POST["rut"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$ocupacion = filter_var($_POST["ocupacion"], FILTER_SANITIZE_STRING);
$direccion = filter_var($_POST["direccion"], FILTER_SANITIZE_STRING);
$ciudad = filter_var($_POST["ciudad"], FILTER_SANITIZE_STRING);
$telefono = filter_var($_POST["telefono"], FILTER_SANITIZE_NUMBER_INT);
$user_Website = "";
//additional php validation
if(strlen($nombre)<3) // If length is less than 3 it will throw an HTTP error.
{
$output = json_encode(array('type'=>'error', 'text' => 'El campo debe tener al menos 5 letras'));
die($output);
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) //email validation
{
$output = json_encode(array('type'=>'error', 'text' => 'Por favor ingrese un email valido'));
die($output);
}
if ($_POST["userWebsite"]) {
$user_Website = $_POST["userWebsite"];
}
/**
* PHPMailer multiple files upload and send example
*/
$mensaje = '';
$mensaje = "Mensaje enviado desde sitio web de Protección Isapres";
$mensaje .= "<br>";
$mensaje .= "Datos del interesado: ";
$mensaje .= "<br>";
$mensaje .= "<strong>Nombre: </strong>". $nombre ."<br>";
$mensaje .= "<strong>Rut: </strong>". $rut ."<br>";
$mensaje .= "<strong>Email: </strong>". $email ."<br>";
$mensaje .= "<strong>Ocupación: </strong>". $ocupacion ."<br>";
$mensaje .= "<strong>Dirección: </strong>". $direccion ."<br>";
$mensaje .= "<strong>Ciudad: </strong>". $ciudad ."<br>";
$mensaje .= "<strong>Teléfono: </strong>". $telefono ."<br>";
$mensaje .= "<br>";
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom($to_Email , 'Contacto Sitio Web');
$mail->addAddress($to_Email, $nombre);
$mail->Subject = $subject;
$mail->Body = $mensaje;
$mail->IsHTML(true);
if (array_key_exists('carta', $_FILES)) {
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['carta']['name']));
if (move_uploaded_file($_FILES['carta']['tmp_name'], $uploadfile)) {
$mail->addAttachment($uploadfile,$_FILES['carta']['name'], 'base64');
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
if (array_key_exists('sobre', $_FILES)) {
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['sobre']['name']));
if (move_uploaded_file($_FILES['sobre']['tmp_name'], $uploadfile)) {
$mail->addAttachment($uploadfile,$_FILES['sobre']['name'], 'base64');
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
if(!$mail->send())
{
echo '<script language="javascript">window.history.back();</script>';
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
echo '<script language="javascript">alert("Mensaje enviado correctamente"); window.history.back();</script>';
$output = json_encode(array('type'=>'message', 'text' => 'Hola '. $nombre .' Gracias por contactarnos.'));
die($output);
}