简体   繁体   English

如何使用一个函数的输出作为另一个函数的输入?

[英]How to use a function's output as another function's input?

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).这个$_POST['emailRemitente']是用户输入,问题是verificarEmails()函数不起作用(它允许使用表单发送格式错误的电子邮件)。

Update:更新:

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>';}
}
function verificarEmails($rte) {
    //$_POST['emailRemitente'] = $rte;
    if (!filter_var($rte, FILTER_VALIDATE_EMAIL)) {
        echo "<br>Oops! el mail debe ser correcto";
    } else { 
        return $rte;
    }
}
//another function
function func2($value2) {
     return verificarEmails($value2);

}

echo func2('jack@gmail.com');

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 :2个功能

<?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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM