I have this function to check the formal correctness of the user's email in a webform, in order to send an email to the webmaster using the user's email address.
<?php
function verificarEmails($rte) {
$_POST['emailRemitente'] = $rte;
if (!filter_var($rte, FILTER_VALIDATE_EMAIL)) {
echo "<br>Oops! el mail debe ser correcto";
} else {
return $rte;
}
}
If I want to use the output of that function within a second function (the one that actually sends the email), should I add (inside the second function), something like this?
$emailRemitente = verificarEmails($_POST['emailRemitente']);
What am I doing wrong here? I'm very sure there's this big fat elephant that I'm missing. This $_POST['emailRemitente']
is the user input, and the problem is that the verificarEmails()
function won't work (it allows malformed emails to be sent using the form).
The second function complete is this:
<?php
function enviarMails($correo) {
$miEncabezado = "Estimada,<br>";
$emailRemitente = verificarEmails($_POST['emailRemitente']);
$nombreRemitente = $_POST['nombreRemitente'];
$para = $correo;
$asunto = 'Urgente';
$mensaje = $miEncabezado . $GLOBALS['texto'];
$headers = 'From: '.$nombreRemitente.' '.'<'.$emailRemitente.'>'."\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//mando mail a los usuarios
$envioUsuarios = mail($para, $asunto, $mensaje, $headers);
if($envioUsuarios) {
echo '<br><span class="ok">Mensaje enviado a '.$correo.'</span><br>';
} else {echo'<br><span class="error">No se mandó mail a '.$correo.'</span><br>';}
}
Try this
<?php
function enviarMails($correo) {
if (filter_var($_POST['emailRemitente'], FILTER_VALIDATE_EMAIL)) {
$miEncabezado = "Estimada,<br>";
$nombreRemitente = $_POST['nombreRemitente'];
$para = $correo;
$asunto = 'Urgente';
$mensaje = $miEncabezado . $GLOBALS['texto'];
$headers = 'From: '.$nombreRemitente.' '.'<'.$_POST['emailRemitente'].'>'."\r\n";
$headers. = 'MIME-Version: 1.0' . "\r\n";
$headers. = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//mando mail a los usuarios
if(mail($para, $asunto, $mensaje, $headers)) {
echo '<br><span class="ok">Mensaje enviado a '.$correo.'</span><br>';
} else {
echo'<br><span class="error">No se mandó mail a '.$correo.'</span><br>';
}
} else {
// email not validated
}
}
With 2 functions:
<?php
function verificarEmails($rte) {
if (!filter_var($rte, FILTER_VALIDATE_EMAIL)) {
return false;
} else {
return true;
}
}
function enviarMails($correo) {
if (verificarEmails($_POST['emailRemitente']) {
$miEncabezado = "Estimada,<br>";
$nombreRemitente = $_POST['nombreRemitente'];
$para = $correo;
$asunto = 'Urgente';
$mensaje = $miEncabezado . $GLOBALS['texto'];
$headers = 'From: '.$nombreRemitente.' '.'<'.$_POST['emailRemitente'].'>'."\r\n";
$headers. = 'MIME-Version: 1.0' . "\r\n";
$headers. = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//mando mail a los usuarios
if(mail($para, $asunto, $mensaje, $headers)) {
echo '<br><span class="ok">Mensaje enviado a '.$correo.'</span><br>';
} else {
echo'<br><span class="error">No se mandó mail a '.$correo.'</span><br>';
}
} else {
// email not validated
}
}