By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

php include() inside mail()

P: 24
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $to = "someone2@example.com";
  3. $subject = "Test mail";
  4. $message = include("PROGRAMtoEXECUTE.html"); 
  5. $from = "someone1@example.com";
  6. $headers = "From: $from";
  7. mail($to,$subject,$message,$headers);
  8. echo "Mail Sent.";
  9. ?>
[Please use CODE tags when posting source code. Thanks! --pbmods]

This email contains a message which changes every time it is sent.
This changing message could be the time, some random numbers, etc..
Is there any way to have it run the program and then send the output?
May 28 '07 #1
Share this Question
Share on Google+
2 Replies


bergy
P: 89
Yes, this is possible, however you don't want to use the include() function. Try using file_get_contents()

Your code would look like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $to = "someone2@example.com";
  3. $subject = "Test mail";
  4. $message = file_get_contents("PROGRAMtoEXECUTE.html"); 
  5. $from = "someone1@example.com";
  6. $headers = "From: $from";
  7. mail($to,$subject,$message,$headers);
  8. echo "Mail Sent.";
  9. ?>
I'm almost positive you can use this to get a file at a specific URL as well so if your file is a PHP file that you want to execute you can do a file_get_contents("http://www.mysite.com/php.php"); If that doesn't work, check out cURL on php.net and google. That should give you everything you'd need to know.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $to = "someone2@example.com";
  3. $subject = "Test mail";
  4. $message = include("PROGRAMtoEXECUTE.html"); 
  5. $from = "someone1@example.com";
  6. $headers = "From: $from";
  7. mail($to,$subject,$message,$headers);
  8. echo "Mail Sent.";
  9. ?>
[Please use CODE tags when posting source code. Thanks! --pbmods]

This email contains a message which changes every time it is sent.
This changing message could be the time, some random numbers, etc..
Is there any way to have it run the program and then send the output?
May 28 '07 #2

P: 24
Thank you Bergy!

I tried using file_get_contents(), with the txt, html and php extensions,
but this only gets the text of the program source codes, and does
not execute or run the program first, and then output the results.
For example, Save these 3 lines as "date.php".

<html><head></head><body>
<?php print date("Y-m-d"); ?>
</body></html>

And put "date.php" into the $message.
If you then send the email, you will receive the above 3 lines of code
and not the output of running those 3 lines of code.

I want to email only the program output and not the source code.
Perhaps, I should be looking for a completely different method?
May 29 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.