LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Forward email to script for processing? (https://www.linuxquestions.org/questions/programming-9/forward-email-to-script-for-processing-509573/)

aaronvegh 12-11-2006 10:26 PM

Forward email to script for processing?
 
Hi there,
I'd like to set up a system that processes incoming mail to a specific address that I'll set up. The system might scan the subject string and act accordingly. Specifically, I'd like the system to be controlled by email.

I've had a hard time finding specific information on the 'net about doing this. I'm running Postfix on a Centos box, but I really don't want to mess with configuration of what's already a well-working system.

Can anyone offer specific advice on how to forward the fields of an email to a PHP script for processing?

Thanks in advance,
Aaron.

jantman 12-12-2006 01:03 AM

If you setup procmail for mail delivery, you can tell procmail to pass incoming mail to a user-defined script.

aaronvegh 12-12-2006 07:21 AM

I'm not terribly familiar with procmail. I thought that a procmailrc file had to be sitting in a particular user folder to work -- and was tied to that user's email address? Where would I put it to handle delivery for a non-user-specific account?

Thanks,
Aaron.

Guttorm 12-12-2006 08:17 AM

Hi

You can also use a .forward file in the home directory of the user on that server. Normally, you can put an email address here to have the email forwarded, but you can also specify a script if you prefix it with a "|".

For example:
You want a php script (/usr/local/bin/webmaster-email.php) to get emails sent to "webmaster@yoursite.org".
1) create the user "webmaster" on the server
2) create a /home/webmaster/.forward file and put "|/usr/local/bin/webmaster-email.php" in it (without the quotes)
3) first line of /usr/local/bin/webmaster-email.php should be #!/usr/bin/php (path to your php)
4) chmod 755 /usr/local/bin/webmaster-email.php

The reason it's good to use a system account is that it's nice to control under which user the script will run. You probably don't want to run it as root.

When the script executes, reading from stdin will give you the email.
Code:

#!/usr/bin/php
<?php
$email = "";
$fp = fopen('php:///stdin");
while ($line = fgets($fp,1000))
        $email .= $line;
fclose($fp);
//code to handle email...
?>

The email will include all headers - to find subject, just scan the lines untill you find a line starting with "Subject ".

To get the email's body, scan the lines until you find a blank line. The body is after that line.

And:
Quote:

Where would I put it to handle delivery for a non-user-specific account?
Add to /etc/aliases, e.g:
webslave: webmaster

countrydj 01-12-2011 06:02 PM

Hi Guttorm...
I know that this thread is very old, but it is the nearest thread that I have found to my problem.
If you are still monitoring this thread would you please respond.
I have a problem that I have been trying to reslove for days and can't find and answer.
Regards,

John C

archtoad6 01-13-2011 12:52 PM

That should be: "Please respond here:"
http://www.linuxquestions.org/questi...script-855718/
That's the current theread.

countrydj 01-13-2011 01:31 PM

Thanks moderator.

The thread that you directed me to is MINE.

However, I saw that there was a previous thread and wanted to ask Guttorm a question about it.
I can't find an email address for him or anywhere to send him a message other than here.

John C

archtoad6 01-14-2011 05:41 AM

You're welcome.

We don't allow cross- or double- posting at LQ, so I wanted to make sure that Guttorm answered you in the appropriate place.

countrydj 01-14-2011 06:55 AM

Hi Rick...

I had no intention of cross- or double- posting. I just want to contact Guttorm.
Can you suggest how I might get in touch with Guttorm ???

Thanks,

John C

archtoad6 01-14-2011 09:02 AM

Quote:

Originally Posted by countrydj (Post 4224019)
I had no intention of cross- or double- posting.

Nor did I think you did.
I just wanted to be sure that the same issue wasn't being discussed in 2 threads, so I added a link to the main/current discussion.

I thought posting here was a reasonable way to contact Guttorm. In addition to this, you could leave a VM (Visitor Message) here:
http://www.linuxquestions.org/questi...guttorm-88727/

countrydj 01-14-2011 11:34 AM

Thanks Rick...


All times are GMT -5. The time now is 12:37 PM.