0

I'm trying to send an email through PHP. Originally my $message variable was set to html as well as PHP variables from user input. This works fine--I'm receiving the emails with the correct variables and all.

Then I tried to include some logic which checks to see which service was selected by the user from the form they filled out, and based on that, the contents of the $message variable were changed (i.e. the outputting html content was slightly different).

In order not to have a long file with bunch of html, I decided to move the html code to separate files and set the $message variable = to file_get_contents() . The email sends fine, but my variables are no longer displaying the user input data. I even tried using session_start() at the top of the file where the html template is.

<?php
session_start();

$_SESSION["service"] = $_POST['service'];

if (isset($_POST['submit'])){
  $service = $_POST['service'];
  
  if($_POST['service']=="Service 1"){$message = 'email_template-service-1.php';}
    else $message = file_get_contents("email_template-service-2.php");

    $to = '[email protected]';
    $subject = 'Subject';
    $headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .

    mail($to, $subject, $message, $headers);
    header('Location: /confirmation.php');
}

else {header('Location: /index.php');}
?>

Am I missing something here? TIA!

2
  • Where is the user input data sent? $_POST or $_GET? email_template.php is that just a static template? Show us your complete code, what you have isn't enough to answer your question. If you echo $message what do you get? Commented Feb 27, 2019 at 22:07
  • I edited the post to include the code @DCR Commented Feb 27, 2019 at 22:24

1 Answer 1

1

A better approach would be to just include the file and use ob_get_clean():

ob_start();
if($_POST['service']=="Service 1") {include 'email_template-service-1.php';}
else include 'email_template-service-2.php';
$message = ob_get_clean();

Anything echoed between ob_start() and $message = ob_get_clean() will go into the $message variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.