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

is there a way to initialize php in non-.php files?

n8kindt
100+
P: 221
for every new user our company adds, we give them firstname.lastname@ourdomain.com that forwards to their personal address. as is, this is quite a manual process and i'd rather that this junk is self-automated as there is a great possibility of human error.

currently, the file that holds all the email addresses looks like this...

it starts off like this:
Expand|Select|Wrap|Line Numbers
  1. RECIP=$1
  2. SHELL=/bin/sh
  3. INCLUDERC=.procmailrc.local
and then displays the forwarding data:

Expand|Select|Wrap|Line Numbers
  1. :0
  2. * $RECIP ?? ^^rose.flower=ourdomain.com$
  3. {
  4.     :0
  5.     !rosesemail@charter.net
  6. }
  7.  
  8. :0
  9. * $RECIP ?? ^^laura.person=gurrliegirl.com$
  10. {
  11.     :0
  12.     !laurasemail@pacbell.net
  13. }
  14.  
  15. :0
  16. * $RECIP ?? ^^freddy.jones=gurrliegirl.com$
  17. {
  18.     :0
  19.     !freddysemail@pacbell.net
  20. }
  21.  
as you can see, this is something that i could loop through in php (or any language for that matter--i have no idea what language the above is, however) from a database (mysql). however, the filename has to stay the same as i do not have access to the main config file since this is a hosted server. is there any way to initialize php and then do this looping without a php file extension?? or, can i include a php file into this code somehow? i would rather not edit the file directly b/c that would mean there would still be two spots (my mysql database and this file) with the same exact data.

also, the filename is .procmailrc i'm not sure if that would make a difference in answering this question or not.
Sep 9 '08 #1
Share this Question
Share on Google+
10 Replies

Atli
Expert 5K+
P: 5,058
Hi.

How is that file executed?
Theoretically the file extension shouldn't matter.

If this is executed via a CLI, the file extension wouldn't matter. Like in Linux, you should be able to do:
Expand|Select|Wrap|Line Numbers
  1. $ php path/to/my/file.ext
And that would execute that file as PHP, no matter what extension.

If it is executed via a HTTP server you would simply have to instruct the server to execute that type of file as PHP, just like it does with .php files.
Sep 9 '08 #2

n8kindt
100+
P: 221
Hi.

How is that file executed?
Theoretically the file extension shouldn't matter.

If this is executed via a CLI, the file extension wouldn't matter. Like in Linux, you should be able to do:
Expand|Select|Wrap|Line Numbers
  1. $ php path/to/my/file.ext
And that would execute that file as PHP, no matter what extension.

If it is executed via a HTTP server you would simply have to instruct the server to execute that type of file as PHP, just like it does with .php files.
i have no idea of finding out exactly how it is executed and i'd really have to do some research on this email program to figure that out. i'm thinking (and hoping) it would be alot quicker to do some trial and error. i'll just assume it is CLI for now...

kind of confused what the $ php path/... does. is it pointing to a file to include and then reading it? if so, i should be able to set up an external php file that generates/formats all the data from my database and then include it in my main file.

for example, i could make a file such as path/to/my/file/db.php like this...
Expand|Select|Wrap|Line Numbers
  1. while ($query_data){
  2. echo $query_data['email'];
  3. //obviously not exact, but you get the idea
  4. }

and then in my .procmailrc do this...

Expand|Select|Wrap|Line Numbers
  1. RECIP=$1
  2. SHELL=/bin/sh
  3. INCLUDERC=.procmailrc.local
  4.  
  5. $ php path/to/my/file/db.php
would that be the same thing as the manual hard coding that exists now?
Sep 10 '08 #3

n8kindt
100+
P: 221
Hi.

How is that file executed?
Theoretically the file extension shouldn't matter.

If this is executed via a CLI, the file extension wouldn't matter. Like in Linux, you should be able to do:
Expand|Select|Wrap|Line Numbers
  1. $ php path/to/my/file.ext
And that would execute that file as PHP, no matter what extension.

If it is executed via a HTTP server you would simply have to instruct the server to execute that type of file as PHP, just like it does with .php files.
oh wow i just re-read that and i obviously missed what you said. i don't have any way to edit the file that executes the file. i only have the supplemental parts to work with. and i could be wrong, but i don't think telling the server to execute it as php would make a difference b/c the server never actually executes the file. i believe this program reads whatever data is in the file and that's it. i'm just wondering if there is a way to include data from a file executed in php into a file such as this or somehow embed php script into this file.
Sep 10 '08 #4

n8kindt
100+
P: 221
let me put it another way. i actually came up with this one from another problem i'm dealing with right now that is very similar. i have a javascript (.js) file that needs the image names/locations generated from a mysql database. i would need to use php to do gather the info from the database. how would i feed the php generated image names/locations into the .js file? my current setup is less than desirable. there has to be some sort of include function in javascript, right? same goes with my other scenario?

of course, i'm merely speculating at this point. i'm probably nowhere near the real procedure to do this.
Sep 12 '08 #5

Atli
Expert 5K+
P: 5,058
Generating JavaScript files with PHP is no problem.

You can create .php file that outputs JavaScript, and then include that file as a .js file in your page HTML.

Like, say:
Expand|Select|Wrap|Line Numbers
  1. # JavaScript.php
  2. <?php 
  3.   // Let the browser know this is supposed to be JavaScript.
  4.   header("Content-Type: text/javascript"); 
  5. ?>
  6. function ShowRandom() {
  7.   alert("<?php echo MD5(mt_rand()); ?>");
  8. }
  9.  
Expand|Select|Wrap|Line Numbers
  1. # Your main HTML file
  2. <html>
  3.   <head>
  4.     <script type="text/javascript" src="JavaScript.php"></script>
  5.   </head>
  6. <body onload="javascript: ShowRandom();">
  7. </body>
  8. </html>
  9.  
Which would show the MD5 hash generated by PHP in a JavaScript alert window.

I don't really know what to do with the other file you were talking about earlier. I don't recognize the language it is using so I can't really say how to use it.

Would it not be possible to simply edit that file using PHP?
You know the syntax, and as I understand it, you are simply trying to automate the process of altering that file. Why not simply alter the file itself, rather than try to dynamically create it each time it is read?
Sep 12 '08 #6

n8kindt
100+
P: 221
Generating JavaScript files with PHP is no problem.

You can create .php file that outputs JavaScript, and then include that file as a .js file in your page HTML.

Like, say:
Expand|Select|Wrap|Line Numbers
  1. # JavaScript.php
  2. <?php 
  3.   // Let the browser know this is supposed to be JavaScript.
  4.   header("Content-Type: text/javascript"); 
  5. ?>
  6. function ShowRandom() {
  7.   alert("<?php echo MD5(mt_rand()); ?>");
  8. }
  9.  
Expand|Select|Wrap|Line Numbers
  1. # Your main HTML file
  2. <html>
  3.   <head>
  4.     <script type="text/javascript" src="JavaScript.php"></script>
  5.   </head>
  6. <body onload="javascript: ShowRandom();">
  7. </body>
  8. </html>
  9.  
Which would show the MD5 hash generated by PHP in a JavaScript alert window.

I don't really know what to do with the other file you were talking about earlier. I don't recognize the language it is using so I can't really say how to use it.

Would it not be possible to simply edit that file using PHP?
You know the syntax, and as I understand it, you are simply trying to automate the process of altering that file. Why not simply alter the file itself, rather than try to dynamically create it each time it is read?
ah, i like that javascript trick. that is slick. much better than having all the messy javascript displayed directly in the page source.

yes, i have considered editing it directly in php. it looks like i may have to settle for that although it is not the most ideal solution. i was hoping that i could allow my users to edit the database in a linked table in microsoft access. so this could get messy if i can't figure out what language this file is in and generate the data from mysql. i'll just have to do some research i guess! i appreciate your replies, Atli. you helped me out a bunch!
Sep 15 '08 #7

pbmods
Expert 5K+
P: 5,821
Heya, n8kindt.

The file you are editing is a procmailrc file.

You can execute shell scripts inside of Procmail "recipes" (for example, calling a PHP script and passing off the email to that script).

Replace all those individual email checks with:

Expand|Select|Wrap|Line Numbers
  1. :0:
  2. | /usr/local/bin/php /path/to/processing/script.php
  3.  
Replace '/usr/local/bin/php' with the actual path to the PHP binary (use `which php` if you don't know) and '/path/to/processing/script.php' with the PHP script that you want to use to connect to the database to process the email.

This will cause your email server to pass every single email to the PHP script, which will then be responsible for final delivery (most likely using the mail() function).

As this represents the extent of my knowledge, I will go ahead and move this thread to the Linux forum so that our resident Linux Experts will be able to help you out should you run into any trouble.
Sep 17 '08 #8

n8kindt
100+
P: 221
Heya, n8kindt.

The file you are editing is a procmailrc file.

You can execute shell scripts inside of Procmail "recipes" (for example, calling a PHP script and passing off the email to that script).

Replace all those individual email checks with:

Expand|Select|Wrap|Line Numbers
  1. :0:
  2. | /usr/local/bin/php /path/to/processing/script.php
  3.  
Replace '/usr/local/bin/php' with the actual path to the PHP binary (use `which php` if you don't know) and '/path/to/processing/script.php' with the PHP script that you want to use to connect to the database to process the email.

This will cause your email server to pass every single email to the PHP script, which will then be responsible for final delivery (most likely using the mail() function).

As this represents the extent of my knowledge, I will go ahead and move this thread to the Linux forum so that our resident Linux Experts will be able to help you out should you run into any trouble.
oh wow, that is great information! thank you so much for all your help! i feel like i'm on the right track now.

alright, well i already foresee one thing that i will need to know. how do i extract all the email information (body, subject, etc) in order to send it to php from there? or is there a way to set it up so that php evaluates the email address and bounces back the required information to procmail?
Sep 17 '08 #9

pbmods
Expert 5K+
P: 5,821
Hm....

That one's tricky. I think Procmail hands it off to PHP and then says, "Ok, have fun. I'm done with you now."

You can test it out by creating a simple PHP script:

Expand|Select|Wrap|Line Numbers
  1. file_put_contents('/some/path/test.txt', print_r($argv, true));
  2.  
Then send an email to an address hosted on your mail server and open the test.txt file that the script created (you might want to change the '/some/path' part). You should get a pretty good idea of what PHP is getting from Procmail.

In terms of handling the email itself, and I could be wrong (it's been known to happen before), but I think you'd need to use mail() to finish sending the email.

I'll go ping the Linux Experts and see if any of them knows any more.
Sep 17 '08 #10

n8kindt
100+
P: 221
Hm....

That one's tricky. I think Procmail hands it off to PHP and then says, "Ok, have fun. I'm done with you now."

You can test it out by creating a simple PHP script:

Expand|Select|Wrap|Line Numbers
  1. file_put_contents('/some/path/test.txt', print_r($argv, true));
  2.  
Then send an email to an address hosted on your mail server and open the test.txt file that the script created (you might want to change the '/some/path' part). You should get a pretty good idea of what PHP is getting from Procmail.

In terms of handling the email itself, and I could be wrong (it's been known to happen before), but I think you'd need to use mail() to finish sending the email.

I'll go ping the Linux Experts and see if any of them knows any more.
thank you for all your help! i will be testing out your suggestion shortly.
Sep 18 '08 #11

Post your reply

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