Sending the form data for two pages

Asked

Viewed 50 times

1

Good morning! I have 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 in the template too, how can I do this? I want the form data sent to both the controller and the email template.

Archive of Contactocontrol.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: https://pastebin.com/FtjpctfH

  • You want form data to be entered in the body of the email?

  • Hi Whatyson, yes

  • could put the content of the email?

  • Hi Marcus, the content of the email as well as the controller are there in the question, I edited and added them

1 answer

0


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

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

And the function can look something like this:

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

Then you just bind the text before sending the email:

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

That is, the function will take your string (in this case, the $body_message) and replace the data :variable: for the amount that comes from the form.

  • 1

    Muuuuuito thanks Whatyson! helped me more!!!

  • For nothing @Cauesilvestre , note that if there is any data in the body of the email that is equal to :key: in your array it will also be replaced, that is, review the body of the email and the keys you are using in your array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.