Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/257115-php-email-script-how-do-i-include/
Share on other sites

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

 

 

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.

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;

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?

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();

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.