Igor Smirnov Igor Smirnov - 1 year ago 97
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:

* 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>
<div class="col-lg-7">

<?php get_footer(); ?>

mailer.php - the handler for the form:

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


$mail = new PHPMailer();

$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->Subject = $fname;

$body = file_get_contents("test.php");
echo "Everything is okay";
echo "Error!";


test.php - html markup for letter:

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

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 Source

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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download