leszer Posted February 16, 2012 Share Posted February 16, 2012 Trying to use an include() in a script to send an email, not working out. Basically I have a pretty drastic page I've formatted out and I want it to be shipped out in an email in that format. I thought it would be simple to get an include in a php code to email but the email works.. and doesn't have anything in it. I've tried with and without quotes, any help appreciated. <html> <body> <? //change this to your email. $to = "me@me.com"; $from = "me@me.com"; $subject = "stuff"; $message="<?php include("another.php"); ?>" $headers = "From: $from\r\n"; $headers .= "Content-type: text/html\r\n"; //options to send to cc+bcc //$headers .= "Cc: [email]maa@p-i-s.cXom[/email]"; //$headers .= "Bcc: [email]email@maaking.cXom[/email]"; // now lets send the email. mail($to, $subject, $message, $headers); echo "Message has been sent....!"; ?> </html> </body> Quote Link to comment https://forums.phpfreaks.com/topic/257115-php-email-script-how-do-i-include/ Share on other sites More sharing options...
ScotDiddle Posted February 16, 2012 Share Posted February 16, 2012 Ieszer, Your extra php delimiters are getting in the way. My IDE shows a syntax error... I'm surprised it ran at all... Try: $message = include("another.php"); echo $message; Scot L. Diddle, Richmond VA Quote Link to comment https://forums.phpfreaks.com/topic/257115-php-email-script-how-do-i-include/#findComment-1318050 Share on other sites More sharing options...
ManiacDan Posted February 16, 2012 Share Posted February 16, 2012 When you include a PHP file, it has the exact same effect as copying and pasting that PHP file directly into your code. You cannot include a file in the middle of a line like you're trying to do. What does the email generation file do? You should wrap it in a function which returns a string, then simply include the file at the top of your script, and call the function when you need it. Quote Link to comment https://forums.phpfreaks.com/topic/257115-php-email-script-how-do-i-include/#findComment-1318052 Share on other sites More sharing options...
Psycho Posted February 16, 2012 Share Posted February 16, 2012 You can't put a PHP block inside another PHP block. You already put an opening tag for PHP processing right here <? //change this to your email. (Although you should NOT be using short tags!) So, when the PHP parser get here $message="<?php include("another.php"); ?>" It's going to be confused because the PHP parser was already initiated. However, you *could* do this: $message = include("another.php"); Although that can work I doubt it will work for you with your current file. An include file can be configured to "return" a value, but it is not very common so I doubt you configured it in that way. You would need to build up the content in that file as a variable, and then at the end do a return $email_content; Quote Link to comment https://forums.phpfreaks.com/topic/257115-php-email-script-how-do-i-include/#findComment-1318053 Share on other sites More sharing options...
leszer Posted February 16, 2012 Author Share Posted February 16, 2012 You're saying I have to go wrap my whole another.php script in a function to be returned in the email script? I was hoping for a way around that because the another.php script is a file which is including the scripts of 3 other pages and formatting them into one style. Would I then have to go back to those files and wrap them in functions also? Quote Link to comment https://forums.phpfreaks.com/topic/257115-php-email-script-how-do-i-include/#findComment-1318060 Share on other sites More sharing options...
ManiacDan Posted February 16, 2012 Share Posted February 16, 2012 What, exactly, does another.php do? Does it render an entire web page? If that's the case, you may be able to do something like: ob_start(); include('another.php'); $message = ob_get_clean(); Quote Link to comment https://forums.phpfreaks.com/topic/257115-php-email-script-how-do-i-include/#findComment-1318063 Share on other sites More sharing options...
leszer Posted February 16, 2012 Author Share Posted February 16, 2012 Yes it does and that worked perfectly. So glad I don't have to go back and nest all that in a function. Thank you very much! What, exactly, does another.php do? Does it render an entire web page? If that's the case, you may be able to do something like: ob_start(); include('another.php'); $message = ob_get_clean(); Quote Link to comment https://forums.phpfreaks.com/topic/257115-php-email-script-how-do-i-include/#findComment-1318079 Share on other sites More sharing options...
ManiacDan Posted February 16, 2012 Share Posted February 16, 2012 include files should not really be done like this. include files should contain functionality that your page needs, like a database class or a function that generates the emails. If you start making include files that indiscriminately spit data out to the user's screen, you run into...this exact issue. Quote Link to comment https://forums.phpfreaks.com/topic/257115-php-email-script-how-do-i-include/#findComment-1318107 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.