Dmitry Shulga
Dmitry Shulga

Reputation: 618

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("[email protected]", "Name");
$mail->WordWrap = 50;
$mail->SetFrom("[email protected]", "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!

Upvotes: 1

Views: 259

Answers (3)

Kiran Krishnan
Kiran Krishnan

Reputation: 30

You can do Heredoc syntax to define the html text along with variables. file_get_contents just read the text source of the file and store in your variable. You have to include this file wherever it needed and use heredoc syntax :

$body = <<<HTML

<h5>First Name of Person:</h5>
{$fname} 

HTML;

Of you can define your test.php with some template tags . Use some template variables and replace the value with the actual variable in your form processing code . In test.php, use

<h5>First Name of Person:</h5>
{first_name} 

Then after getting the contents using file_get_contents, replace template variables with the value received from POST .

$body = file_get_contents("test.php");
$body = str_replace("{first_name}",$fname,$body);

Do this for all values to be replaced .

Upvotes: 0

Duane Lortie
Duane Lortie

Reputation: 1260

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;

Upvotes: 0

Stan
Stan

Reputation: 26501

You can store it in session on your POST request and then display it. Also, in WP sessions are not enabled by default. You need to enable them like so.

if (!session_id()) {
    session_start();
}
  • $_SESSION['firstName'] = $_POST['first-name']; // Store
  • <?php echo $_SESSION['firstName']; // Display ?>

Upvotes: 1

Related Questions