iwato Posted March 24, 2017 Share Posted March 24, 2017 BACKGROUND: I am setting up an email verification routine that receives $_POST data from a form and sends a confirmation email to the applicant with a verification link. The form and the PHP processing routines that store the $_POST data in a MySQL database, and that send out the confirmation email in text format are contained in the same document. The HTML formatted confirmation email is contained in an external file and is included using the following PHP-Mailer method: $mail->msgHTML(file_get_contents('../../confirmation_mail.html'), dirname(__FILE__)); GOAL: Include in the external HTML file information obtained from the $_POST variable before the email is sent. WHAT DOES NOT APPEAR TO WORK: I have tried placing statements similar to <?php echo "blah, blah, blah" . $variable_name . "blah, blah, blah"; ?> in the external file in the hope that the value of $variable_name would be read into the file before it is sent. Unfortunately, this does not work. ANY SUGGESTIONS? Link to comment Share on other sites More sharing options...
Ingolme Posted March 24, 2017 Share Posted March 24, 2017 You can use output buffering (ob_start() and ob_get_clean()) and include() to make use of an HTML template and store it in a variable. The template will have access to all the variables in the scope that include was called in. Link to comment Share on other sites More sharing options...
iwato Posted March 24, 2017 Author Share Posted March 24, 2017 Great! I just reviewed how to use buffering. My first real application! I am excited. Could you please provide me with a brief work flow to help me get started? What get's buffered? And, where should the include() take place? By the way, Ingolme, I am very happy that you responded and to see that you you are still with W3Schools. It has been a very, very long time! Roddy Link to comment Share on other sites More sharing options...
Ingolme Posted March 24, 2017 Share Posted March 24, 2017 A short and simple example would be this: // Template variables $name = 'John'; // Buffering ob_start(); include 'template.php'; $message = ob_get_clean(); // Send mail mail('john@example.com', 'Good evening', $message); template.php would look like this: <p>Hello, <?php echo $name; ?>.</p> <p>Thank you for signing up.</p> Link to comment Share on other sites More sharing options...
iwato Posted March 25, 2017 Author Share Posted March 25, 2017 Got it! Thanks! Roddy Link to comment Share on other sites More sharing options...
iwato Posted March 26, 2017 Author Share Posted March 26, 2017 OK. I have tried what you suggested, and this was the result. Firstly, I am not using PHP mail( ) per se; rather, I am using PHPMailer. Thus, I was not able to include the message where you suggested. Secondly, if I use ob_end_clean( ), nothing happens. If I use ob_end_flush( ); however, I can at least get the file with the desired value to appear on the form page that sends the message, but not in the sent email. Any suggestions? Link to comment Share on other sites More sharing options...
Ingolme Posted March 26, 2017 Share Posted March 26, 2017 My code was using ob_get_clean() which deletes the buffer and returns its contents. It doesn't matter what code you use to send the mail, the string that you want to use is in the variable, you can do what you want with it: print it, store it in a database, send it in an e-mail... Link to comment Share on other sites More sharing options...
iwato Posted March 27, 2017 Author Share Posted March 27, 2017 As I still could not get it to work, I explored further on the net and came up with this very happy solution at stack overflow. $html_message = file_get_contents('../../confirmation_mail.php'); $html_message = str_replace('%username%', $name, $html_message); . . . $mail->msgHTML($html_message); No buffering required. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now