Sending the data of a form by two pages

1

Good morning! I am having a question, I am sending the data of a form to a Controller that sends an email with such data. However the Controller when mounting the email uses such a function

$body_message = file_get_contents('/email.php');

The email template is very large and so I pull it from another location, however I want to insert variables sent by the form into the template too, how can I do this? I want the form data to be sent to both the controller and the email template.

Contact ArchiveControl.php:

        public function enviarEmail(){
        $dados = array();

        $emailClasse = new Email();

        if(isset($_POST['modulo']) && $_POST['modulo'] == "contato"){

            $nomeCompleto  = $_POST['nome'];
            $telefone      = $_POST['telefone'];
            $empresa       = !empty($_POST['empresa'])  ? $_POST['empresa'] : " Não informado.";
            $cargo         = !empty($_POST['cargo'])   ? $_POST['cargo'] : " Não informado.";
            $email         = $_POST['email_corporativo'];    
            $mensagem      = $_POST['mensagem'];

    $arr = array(
        'properties' => array(
            array(
                'property' => 'firstname',
                'value' => $nomeCompleto
            ),
            array(
                'property' => 'email',
                'value' => $email
            ),
            array(
                'property' => 'phone',
                'value' => $telefone
            ),
            array(
                'property' => 'company',
                'value' => $empresa
            )
        )
    );
    $post_json = json_encode($arr);
    $hapikey = ("XXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXX-XXXXXXXXXXXX");
    $endpoint = 'https://api.hubapi.com/contacts/v1/contact?hapikey=' . $hapikey;
    $ch = @curl_init();
    @curl_setopt($ch, CURLOPT_POST, true);
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
    @curl_setopt($ch, CURLOPT_URL, $endpoint);
    @curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = @curl_exec($ch);
    $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curl_errors = curl_error($ch);
    @curl_close($ch);

    $subject = " xxxx- Contato ";
    $body_message = file_get_contents('/email.html');
    $headers = 'De: '.$email."\r\n";
    $headers .= 'Reply-To: '.$email."\r\n";

    $mail_status = mail('[email protected]',$subject,$body_message,$headers);

            if($respostaEnvio){
                $dados['mensagem'] = "Contato enviado com sucesso. Aguarde que logo te enviaremos uma resposta.";
            }else{
                $dados['mensagem'] = "Não foi possível enviar a mensagem no momento. Tente mais tarde.";
            }

        }

The email code is in the link below the pastebin because it did not fit the question: link

    
asked by anonymous 26.04.2018 / 22:04

1 answer

0

You can build a function to bind the data you want to replace, for example, you build an array (which you already have) with the data you want to replace and its values:

$args = [
    ":nomeCompleto:" => $nomeCompleto,
    ":telefone:" => $telefone,
    ":empresa:" => $empresa,
    ":cargo:" => $cargo,
    ":email:" => $email,
    ":mensagem:" => $mensagem,
]

And the function may look something like this:

function bind($str, $args) {
    foreach($args as $key => $arg) {
        $str = str_ireplace($key, $arg, $str);
    }
    return $str;
}

Then you have to bind the text before sending the email:

$body_message = bind($body_message, $args);

That is, the function will get its string (in this case, the $ body_message ) and replace the variable: with the value that comes from the form. p>     

27.04.2018 / 19:58