How to take a variable value by file_get_contents

Asked

Viewed 1,245 times

2

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

file_get_contents('news.php')

On 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 decadastrar the newsletter, but I am not able to rescue the value of the email to pass as parameter, my the link of the news page. php is 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 is 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 couldn’t visualize a solution.

  • you need to send a post with these variables.

  • You just forgot to pass the parameter to the page... Try it this way: file_get_contents("news.php?email=$email"); and change your $_REQUEST to $_GET;

  • Hello @Rafaelwithoeft, I received an error message when passing the parameter as indicated, see: Message body Empty.

  • @adventistapr I believe this to be a Mailer error, no page... check it out: http://stackoverflow.com/questions/13816571/phpmailer-mailer-error-message-body-empty or have you checked what’s coming back from file_get_contents? var_dump(...)

  • Hello @Rafaelwithoeft, now I managed to do following your tip.

  • @adventistapr If you want more security, you can use POST, but if you continue to use GET, I recommend filtering what you get on the page using filter_input (http://php.net/manual/en/function.filter-input.php)

  • Hello @Rafaelwithoeft, I’m using the POST, thanks for the tip.

Show 2 more comments

2 answers

4

You are just ordering the page news php., not sending any email parameter; Try it this way:

$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 for $_GET and don’t forget to filter what you receive using filter_input

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

Documentation: http://php.net/manual/en/function.filter-input.php

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 is just call $_POST as you would in other files, to know their keys use print_r() which in case are id and email.

  • Hello @rray, thanks for the tip, now you are taking the value of the variable, which in the case is an email, but a problem has arisen, not related to change, the rescued email is like this: Monica.martins.2000%40hotmail.com, without the signal of @.

  • @adventistapr, is going as post or get?

  • Hello @rray, is going by POST

Browser other questions tagged

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