Igor Smirnov Igor Smirnov - 3 months ago 9
PHP Question

Get value from another file PHP

I'm using PHPMailer inside WordPress for sending mails. For this I have 3 files:

contacts.php - file with html form:

<?php
/*
* Template Name: Contacts
*/
get_header(); ?>
<div class="container page">
<h2>Напиши нам</h2>
<div class="row">
<div class="col-lg-5">
<form action="<?php bloginfo('template_url');?>/mailer.php" method="post">
<label for="fname">Имя*</label>
<input type="text" class="form-control" id="fname" name="first-name" required>
<label for="tel">Телефон*</label>
<input type="tel" class="form-control" id="tel" required name="phone">
<label for="tel">E-mail</label>
<input type="tel" class="form-control" id="email" name="email">
<label for="">Сообщение*</label>
<textarea required class="form-control" rows="8" name="msg"></textarea>
<button type="submit" class="btn btn-success" id="btn-contacts">Отправить</button>
</form>
</div>
<div class="col-lg-7">

</div>
</div>
</div>
<?php get_footer(); ?>


mailer.php - the handler for the form:

<?php
$fname = $_POST["first-name"];
$phone = $_POST["phone"];
$email = $_POST["mail"];
$msg = $_POST["msg"];

require_once("lib/phpmailer/PHPMailerAutoload.php");

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->FromName = "SomeName";
$mail->Host = "smtp.yandex.ru";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true;
$mail->Username = "username";
$mail->Password = "password";
$mail->AddAddress("someaddress@yandex.ru", "Name");
$mail->WordWrap = 50;
$mail->SetFrom("someaddress@ya.ru", "OwnerName");

$mail->IsHTML(true);
$mail->Subject = $fname;


$body = file_get_contents("test.php");
$mail->MsgHTML($body);
if($mail->Send())
echo "Everything is okay";
else
echo "Error!";

?>


test.php - html markup for letter:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<h5>First Name of Person:</h5>
<?php
echo $fname; //how can I get this variable value???
?>
</body>
</html>


And I have faced with a problem.
How can I get this? (see the third snippet and a comment inside). Thank you in advance!

Answer

Suggest using herdoc syntax because file_get_contents does not automatically translate VARS unless you run eval() on it(I think). So, simply include('test.php'); which contains...

 $body = <<<EOD
 ....
    <h5>First Name of Person:</h5>
 $fname
 ....
 EOD;