How to get value from a variable by file_get_contents

2

I have a script that sends a newsletter, at that moment I get a .php page from

file_get_contents('news.php')

In the news.php page I tried to get the variable using this:

$email = $_REQUEST['email'];

On this page news.php I created a link below the document where the user can register for the newsletter, but I am not able to retrieve the value of the email to pass as a parameter, my the page link news. php looks like this:

Para cancelar o recebimento deste boletim, <a href="http://www.moveissaobento.com.br/unsubscribe.php?e=<?php echo $email ?>"> Clique aqui </a> e vamos removê-lo da lista imediatamente.   

The script that sends the news looks like this:

while($row = $result->fetch_array()) {

$id = $row['id'];
$email = $row['email'];

$mail->setFrom('[email protected]', 'Newsletter');
$mail->addAddress($email);
$mail->Subject = 'Envio Newsletter';
$mail->msgHTML(file_get_contents('news.php'), dirname(__FILE__));
$mail->send();
$mail->ClearAddresses();

$stmt = $mysqli->prepare("UPDATE newsletter SET enviado = 1 WHERE id = $id");
$stmt->execute(); 

}

I could not see a solution.

    
asked by anonymous 17.02.2015 / 19:36

2 answers

2

You can pass variables by post, creating a request like this:

<?php

$post = http_build_query(array('id' => '9999', 'email' => '[email protected]')); //cria o $_POST
$context = stream_context_create(array(
        'http' => array(
                'method' => 'POST',
                'content' => $post,
                'header'  => 'Content-type: application/x-www-form-urlencoded'
        )));



$response = file_get_contents('http://localhost/news.php', false, $context);

$mail->IsHTML(true);
$mail->Body = $response;
$mail->send();
$mail->ClearAddresses();

In news.php, just call $ _POST as you would in other files, to know their keys use print_r() which in this case are id and email.

    
17.02.2015 / 19:59
4

You are just requesting the news.php page, not sending any email parameters; Please try the following:

$mail->IsHTML(true); // Define que o e-mail será enviado como HTML
$mail->Subject  = "Assunto";
$mail->Body = file_get_contents("news.php?email=$email");
$mail->AltBody = "Corpo da Mensagem!";
$send = $mail->Send();
if ($send) {
    echo "E-mail enviado com sucesso!";
}

And change your $_REQUEST to $_GET and do not forget to filter what you receive using filter_input

Exemplo: filter_input(INPUT_GET, 'email', FILTER_SANITIZE_SPECIAL_CHARS);

Documentation: link

  
    
17.02.2015 / 19:52