Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Incoming Mail and PHP

Rated 4.03 (Ratings: 12) (Add your rating)

Log in to add a comment
(53 comments so far)

Want more?

 
Picture of gvtulder

Gijs van Tulder

Member info | Full bio

User since: February 05, 2002

Last login: September 17, 2007

Articles written: 6

We all know how to send email from PHP. Actually, it's quite easy: mail("to@me", "Hello", "Hello"); Handling mail the other way, sending email to PHP is a task much more unknown. In this article, we will write and install a script that we can send an email to.

What do we want?

We want to write and install a script that handles incoming mail. We want our script not to be reachable by a web browser, but by our email client. Sending an email to script@example.com would suffice for running our script and processing the mail.

Why would we want this?

Well, that's a stupid question, because we don't know how to do it and it's fun. You're reading evolt because you want to learn something, aren't you? But this script could be useful. For example:

  • we could write our own mailing list;
  • we could send out a survey by email, that can be filled out by just clicking 'reply';
  • we could manage parts of our site by sending commands by email;
  • etc...

What do we need?

Because the script is triggered by an email, we will have to take a bit of a different approach than when our script is run by someone requesting a web page. Our script has to be started by the mail system. You will need:

  • PHP compiled as a CGI binary, not just as an Apache module;
  • a local mail system or MTA, (are you using Sendmail, Exim, Qmail or some other system);
  • shell access to your server, whether or not you have to be root depends on your mail system.

Step 1: Email to PHP

The first thing we have to do, is tell our mail system to send all mail addressed to script@example.com to our script. How to accomplish this, depends on which mail system you use.

Sendmail

There are two different approaches for forwarding mail to a program in Sendmail. You can create a real user for our script and create a .forward file, or you can create an alias in your aliases file. If you have root access, the latter would be the best option.

If using the aliases file, this can be found in /etc, you add the following line. script is the email alias for our script, /our/script.php should be substituted with the full path to the script.

script: "|/our/script.php"

If using a forward file, create a file named .forward in your home dir. Then add the following line and save the file. Email sent to you, will be forwarded to the script. If you'd also like the email sent to another address, you can add that address before the script and tailed by a comma. /our/script.php should be substituted with the full path to the script.

"|/our/script.php"
or:
myemail@example.com,"|/our/script.php"

Watch out: if you don't add another address to your .forward file, the email is deleted after being sent to the script. You will lose your email.

Exim

There are two different approaches for forwarding mail to a program in Exim. You can create a real user for our script and create a .forward file, or you can create an alias in your aliases file. If you have root access, the latter would be the best option.

If using the aliases file, this can be found in /etc, you add the following line. script is the email alias for our script, /our/script.php should be substituted with the full path to the script.

script: |/our/script.php

You also have to edit your exim.conf, found at /etc/exim.conf. Look for a part containing 'address_pipe'. If there is such a part, replace it with the text below. If there is no such text, paste this text somewhere in exim.conf. Save the file.

address_pipe:
  driver = pipe
  pipe_as_creator

If using a forward file, create a file named .forward in your home dir. Then add the following line and save the file. Email sent to you, will be forwarded to the script. If you'd also like the email sent to another address, you can add that address before the script and tailed by a comma. /our/script.php should be substituted with the full path to the script.

|/our/script.php
or:
myemail@example.com,|/our/script.php

Watch out: if you don't add another address to your .forward file, the email is deleted after being sent to the script. You will lose your email.

Qmail

There are two different approaches for forwarding mail to a program in Qmail. You can create a real user for our script and create a .qmail file, or you can create an alias. If you have root access, the latter would be the best option.

One thing I like about Qmail, is the possibility of creating custom aliases. If you are a normal user, you can create an alias by saving a file called .qmail-foo in your home directory. Email sent to yourusername-foo@example.com is then sent as specified in this file.

If you want to create just an alias, without being an user, eg. myscript@example.com, you create a .qmail file in the /var/qmail/alias directory. .qmail-myscript should do it.

Add the following to your .qmail file and save it. /our/script.php should be substituted with the full path to the script.

|/our/script.php

Step 2: The email

Because the email is passed to the script as a raw email, it is useful to take a closer look at the structure of an email.

Received: from gijs ([192.168.55.2])
	by debian with smtp (Exim 3.12 #1 (Debian))
	id 17AYXZ-0002BE-00
	for ; Wed, 22 May 2002 18:00:41 +0200
From: "Gijs van Tulder" 
To: 
Subject: Test
Date: Wed, 22 May 2002 18:00:41 +0200
Message-ID: <KNEOKJCOCHEDPIMPAIMIOEOJCJAA.gijs@debian>
MIME-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0)
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

This is my message.

Thank you.

The first part of the raw email contains the headers. A header contains information about the email. The header has a name, before the colon, and a value: Header-Name: Value. Important headers are the Subject header, containing the subject of the email, the From header, containing the origin of the email, and the To header, which tells us something about the recipient. The other headers are of minor importance.

An empty line marks the end of the header part. After the first empty line, follows the body of the email.

Step 3: The script

Since our script will function as a shell script, the first line should contain the path to the PHP CGI program. This is most likely located at /usr/bin/php of /usr/local/bin/php. This tells the operating system that this script must be parsed by PHP.

#!/usr/bin/php
<?php

The email is sent to the script through stdin. This is a special 'file' that can be reached by opening php://stdin. We will do that now and read the email.

// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

Now we have the full text of the email in the $email variable, we can start splitting headers from body. We will do that line by line, so the first step would be splitting the email. We also empty the variables that we will fill with the From header, the Subject header, and save other information in.

// handle email
$lines = explode("\n", $email);

// empty vars
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;

We have just set the $splittingheaders variable to true. As long as this variable is true, and we have not yet seen an empty line, the text should be added to $headers. If we find a Subject or a From header, we will save it in the appropriate variable.

After we have seen the first empty line, we have processed the headers and can start adding the lines to $message.

for ($i=0; $i < count($lines); $i++) {
    if ($splittingheaders) {
        // this is a header
        $headers .= $lines[$i]."\n";

        // look out for special headers
        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
            $subject = $matches[1];
        }
        if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
            $from = $matches[1];
        }
    } else {
        // not a header, but message
        $message .= $lines[$i]."\n";
    }

    if (trim($lines[$i])=="") {
        // empty line, header section has ended
        $splittingheaders = false;
    }
}

?>

We now have the headers, the message, the From and Subject information and can save these in a database. You could also use the mail() function to send an automatic reply. That's up to you.

Step 4: Copying

Now you wrote and saved the script, you have to copy it to its final destination, the location you configured in your mail system. You should also make the script executable: chmod 755 Remember: it's a shell script, not a web page.

The process

When an email arrives at the address you configured, it is piped to the script we just wrote. The script processes the headers and the message and returns is in a few useful variables. You can use this as a first building block to write your own mailing list, or do something else with it.

As usually, you can copy and paste the parts of this script, or download it as a text file.

Gijs van Tulder is a Dutch student. He likes to play with all things web related and fancies himself as a part-time amateur web developer.

HTML

Submitted by arnaud on May 31, 2002 - 05:15.

And to make an HTML message, you must add content: text/html in the headers...

login or register to post comments

question

Submitted by pedrito on June 6, 2002 - 19:31.

I see sendmail.cw and sendmail.ct files in /etc, but no aliases file. What is it called? (Or should I create one?) Thanks! I've been wanting to do something like this for a while!

login or register to post comments

Sendmail aliases

Submitted by gvtulder on June 7, 2002 - 02:18.

Pedrito, it looks like sendmail can also have its aliases-file in /etc/mail. Maybe you can find it there?

login or register to post comments

Mime attachments

Submitted by psaportal on August 30, 2002 - 22:43.

I don't see any information here about mime attachments - wouldn't code of the nature being discussed here need to deal with attachments?

login or register to post comments

Mime attachments

Submitted by gvtulder on August 31, 2002 - 03:03.

While you could easily implement attachment handling in this script, you don't need to. The script in this article doesn't modify the incoming email. It splits header and body of the message, that's all.

This script is just the first step in the handling of an incoming email. It saves the headers in the $headers variable, extracts the $from and $subject field from that headers and saves the rest of the message, the body, in $message. You can then use these values in your own script, ie. your MIME attachment extracting script.

(You could, for example, use the PEAR Mail_mimeDecode class to parse MIME messages.)

login or register to post comments

please help me debug

Submitted by openreading on September 30, 2002 - 18:04.

i'm getting a bounceback from sendmail. in your opinion, does the following error message:

smrsh: script.php4 not available for sendmail programs
554 5.0.0 "|/path/script.php4"... Service unavailable


mean:
a.) script not found (i flubbed the path somehow)
b.) sendmail doesn't have access to that file (i set it to 0777, but it might still be in a directory it can't see)
c.) sendmail won't run PHP as a CGI
d.) something you can't tell from this little information,
and/or
e.) something else entirely?

login or register to post comments

Sendmail question

Submitted by thefuzzball on October 28, 2002 - 14:43.

Is it possible for sendmail to route ALL email to this script with out creating aliases for every user (i.e. my server has 8000+ users on it)? Also, would sendmail first parse the server rulles -- reject all for this.domain, accept all for my.domain, relay all for this.domain.too before passing mail on to this script?

login or register to post comments

includes problems & error capturing

Submitted by jmeridth on May 21, 2003 - 06:18.

I've created the script from the above code and have not been able to use any includes in this script. Upon doing so, the script does not work. I'm also new to error capturing in PHP. How would a developer capture things such as a simple parsing error in their script? My script is over 500 lines of code and I'd really like to be able to narrow the error down a bit. =) Thank you for any help.

login or register to post comments

Failure Notice

Submitted by vishal on May 22, 2003 - 11:47.

I am using Exim and added the following line to the alias file

test: |/home/path/to/script.php which executes and works great.

but the only thing is i receive a Failure Delivery Message bcoz exim tries to send message to this test: |/home/path/to/script.php statement

The address_pipe configuration i have is


address_pipe:
virtual_address_pipe:
pipe_transport = virtual_address_pipe
pipe_transport = virtual_address_pipe
pipe_transport = virtual_address_pipe
pipe_transport = address_pipe
pipe_transport = address_pipe


How do i stop the Failure Messages?

login or register to post comments

Piping emails using postfix

Submitted by tomster on June 26, 2003 - 03:15.

I have been trying to pipe incoming mails to postfix to a php script using a few variations but to no avail (in my alias file)

emailaddress: (followed by one of the following)

| "/path/to/script/myscript.php"
"|/path/to/script/myscript/php"
| "/usr/bin/php /mypathagain/script/php"
"|/usr/bin/php /mypathagain/script/php"

I get no error messages, no bounced emails etc but at the same time no results - on activation my script should write a file to disk to show it was executed. It works from the command line e.g.

$ echo "some data" | mysscript.php

and postfix appends the incoming email to a file if i just put in aliass emailaddress: | somefile

so what on earth is wrong? I am at a loss. If anyone can help I will be very grateful.

login or register to post comments

update to previous messages

Submitted by tomster on June 26, 2003 - 08:40.

To rub salt into the wound, I have now solved this problem. If anyone else has similar problems, make sure that your permissions are set properly - using this method postfix does pipe correctly, but if you need ouput to a file, make sure the file is in a directory with write permissions for others.

login or register to post comments

what a mess! :)

Submitted by risto on August 20, 2003 - 07:54.

Returned mail ----- The following addresses had permanent fatal errors ----- |/var/www/vif/script.php (reason: Service unavailable) (expanded from: ) ----- Transcript of session follows ----- smrsh: "script.php" not available for sendmail programs (stat failed) 554 5.0.0 Service unavailable I dont get this!!!

login or register to post comments

Couple of tips for sendmail users

Submitted by stew on September 26, 2003 - 06:26.

I was frustrated by many of the problems reported in the posts above, a couple of notes for everybdy in the same boat

1)
I had an /etc/aliases and an /etc/mail/aliases
I found by adding a line in /etc/aliases rather than /etc/mail/aliases in this format
alias: "|/usr/bin/php /full/path/to/script.php"

2)
Make sure that smrsh (the send mail shell can access your PHP executable by creating a symlink in /etc/smrsh
 ln -s /usr/local/bin/php /etc/smrsh/php

login or register to post comments

RE: Failure Notice

Submitted by rizzo on January 26, 2004 - 21:09.

See Vishal's comment above .

Using EXIM I was successfully passing the mail to the script but was also getting delivery failure errors returned to the sender of the original email.
I added the following line to the very end of the script.

return NULL;

My guess as to why this works: The reason for this is that EXIM on my server is configured to send ANY output from a script which received an email back to the sender as a delivery failure error. I can only assume at this point that Tulder's script as is returns 0 or 1. By explicitly telling the script to return no values it produces no output and therefore exim doesn't send the failed delivery report.

login or register to post comments

How receives mail by php

Submitted by rsmoreira on March 13, 2004 - 04:03.

Hello, I'm building a mail server using php. It's a php script that connects to a mysql to load information. The http server is apache and I work only in Windows. It sends email using mail(....) function, how can I receive the messages??? Do i have to configurate my smtp server???

login or register to post comments

Problem with POSTFIX-MYSQL piping to a script

Submitted by tomster on March 31, 2004 - 22:41.

This problem is really flumoxing me. If anyone can suggest anything... I have postfix installed with MySQL and am using alias_maps to point to a MySQL table to map usernames onto another address. So say you email me@mydomain.com, the table would have the columns and values: username=me and to_address = forward_address. This works fine, as I have tested it out and it maps correctly to whatever address I put in there, *unless* I put a pipe in to a php script, which is what I need. Looking at the warnings log for postfix it complains it cannot connect to the mysql socket on localhost. It also complains there is no postmaster alias. So I added one, and guess what, it now sends all mail to the address I chose postmaster to alias to, but *Still* complains that it could not access the database. This makes no sense. It is accessing the database as it would not be able to obtain the aliases for postmaster etc. However, as soon as the to_address is along the lines of |/usr/bin/php-cgi /path/to/my/script it gets *delayed*, and the originating server keeps retrying it. This pipe works from the original aliases database (i.e. the hash versiion *not* mysql) without a problem, if an email address is in to_address it works, so what on earth am i missing out here?

login or register to post comments

Processing incoming mail on Postfix

Submitted by Trevor on April 6, 2004 - 03:16.

My email server is Postfix running on Apache under linux. After much effort the postmaster successfully managed to create an alias for an email address and point it at "script.php" which runs successfully to a point.
We included a function to forward an email indicating the mail had been received and the script executed. This email is despatched fine. However, it appears that the incoming email content is not placed in "PHP://stdin" by Postfix because the script is unable to read anything from that source, hence none of the rest of the example script does anything.

Can anyone help?? Please?

login or register to post comments

Incoming Mail and PHP

Submitted by nainil on April 6, 2004 - 19:59.

Hi,

I was visiting this forum:
http://forums.devshed.com/archive/t-96700

It too has the same issue ..with some comments on it. I hope it helps.

login or register to post comments

Mail Delivery Failure

Submitted by jasondubya on June 23, 2004 - 00:54.

I copied the script exactly, and then added a quick auto-reply feature to test it's functionality. Everything works great except for one thing. I receive a Mail Delivery failure message that just shows the output of the script.

I modified the first line to the following, but I still receive the failure message.
#!/usr/bin/php -q

Any ideas as to why I am getting this and what I can do to fix it?

login or register to post comments

#!/usr/bin/php

Submitted by kasimirk on August 10, 2004 - 07:29.

I found out (after hefty head scratching...) that after #!/usr/bin/php there should be unix linebreak. So after uploading the script, I log on the box, launch pico, remove and re-enter the line break and things start working (especially if I remember to chown the script to root, and chmod 755 :-).

login or register to post comments

smrsh: script.php4 not available for sendmail prog

Submitted by itsterry on January 11, 2005 - 05:10.

After several hours playing with this, and scouring the web, I finally made it work.

As I couldn't find a clear post on it, I hope the following will be useful.

The problem on my server was that smrsh wasn't allowing sendmail to use php.

Although I saw several posts saying "put a soft link to php in /etc/smrsh", I didn't have an /etc/smrsh. Creating one and putting a soft link to php in it didn't work.

Eventually, I did "man smrsh". The 2nd paragraph said "Briefly, smrsh limits programs to be in the directory /usr/adm/sm.bin, allowing the system administrator to choose the set of acceptable commands".

So I went to /usr/adm/sm.bin and typed
ln -s /usr/bin/php php

After that, sendmail could get to php, and everything worked !

Now I'm off to make the php script do something useful... : - )

login or register to post comments

Forward All Emails

Submitted by mans on January 26, 2005 - 16:44.

Does anyone know how to forward all emails sent to a specific domain on a shared hosting enviroment running exim to php script? Thank you.

login or register to post comments

qmail Success!

Submitted by philscan on November 18, 2005 - 01:56.

Thanks to all of you, your comments have helped me.

This will write to a textfile: email address: blog@example.net

Contents of .qmail-example:net-blog
|/usr/local/bin/php -q /usr/home/pscan/blogATexampleDOTnet_mail_proc.php

Here is the script, blogATexampleDOTnet_mail_proc.php, taken from script in this article and modified to write to file (initial test).

<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!
feof($fd)) {
    
$email .= fread($fd, 1024);
}
fclose($fd);
$outFile = fopen("/usr/home/pscan/emailResult.txt","w");
fwrite($outFile,$email);
fclose($outFile);
?>

Note that the shebang line is removed, and instead, path to php is put in the .qmail.. file.

I created the file emailResult.txt initially, then set permissions on emailResult.txt to be world-writable. This will overwrite it every time an email comes in. you can use fopen() with "a" to append.

It finally worked!

login or register to post comments

Apache Module

Submitted by anjanesh on December 26, 2005 - 20:21.

Is this now possible to get this working with PHP as Apache Module ?

login or register to post comments

why I can not see the complete article?

Submitted by kavehsat on March 13, 2006 - 12:40.

The last paragraph that I can see is the following please help me by sending the article to my email dearkaveh@gmail.com, thank you! We have just set the $splittingheaders variable to true. As long as this variable is true, and we have not yet seen an empty line, the text should be added to $headers. If we find a Subject or a From header, we will save it in the appropriate variable. After we have seen the first empty line, we have processed the headers and can start adding the lines to $message. for ($i=0; $i

login or register to post comments

Re: why I can not see the complete article?

Submitted by dmah on March 13, 2006 - 15:44.

I think that it is fixed now. The less than sign in the for loop was causing a problem. Adding spaces around it seems to have fixed it up. Thanks for the heads up.

login or register to post comments

exim gotcha

Submitted by TheKorn on May 30, 2006 - 15:06.

If you're tearing your hair out trying to get this to work with exim, there's a gotcha... If your script name has an underscore in it, it will ALWAYS generate an MTA failure message!

Except if you wrap the script name with quotes. Bizarre? You bet. Should you care? Probably not; just wrap the script name in quotes and you'll be golden!

Example:

Bad: script: |/our/stupid_script.php

Good: script: |"/our/stupid_script.php"

login or register to post comments

delivery failure errors

Submitted by jswright61 on July 3, 2006 - 14:03.

My system also uses EXIM. I added the return NULL; line, but I still get delivery failure notices (the php script receives the email and processes it correctly, but the sender receives a delivery failure notice. Any suggestions? Scott

login or register to post comments

Thank you!!

Submitted by iduncan on October 1, 2006 - 02:40.

Thank you!! This tutorial was just what I was looking for!

login or register to post comments

HTML Formatted E-Mails

Submitted by steelo on November 4, 2006 - 13:39.

If it receives E-Mails formatted in HTML, it doesn't catch the message properly. I've tried to filter out the extra headers but I can't manage it.

------=_Part_6485_7914052.1162641263809 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline

Just a test.

------=_Part_6485_7914052.1162641263809 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline

Just a test.

------=_Part_6485_7914052.1162641263809--

login or register to post comments

Now sending of messages by

Submitted by reklamabestcom on December 26, 2006 - 11:25.

Now sending of messages by scripts gets under filters, it is better to send email through gmail

login or register to post comments

condredirect

Submitted by PIlias on February 2, 2007 - 19:27.

two comments.

#1. I couldn't get the above to work on my linux system until I: a. removed the line fclose($fd); b. made sure that the .qmail file was read only (i.e. chmod 400)

Otherwise, it works great. second comment.

I tried getting this to work with .qmail's conredirect. this is a utility which will conditionally redirect the email to another address if your program/script exits with a certain error code. According to the .qmail manual, if the program exits with code 0, the email is redirected to another address and execution stops in the .qmail file. if the program exits 99, the .qmail instructions are executed normally. In my .qmail file, I used this:

|condredirect bounceback@domain.com /bouncebacks.php

./Maildir/

How this should work (from what I understand). qmail runs the bouncebacks.php script with the message on stdin. In the bouncebacks.php script, I test the subject for a typical bounceback subject heading (i.e. failure, undeliverable, etc). If the file finds that the email bounced, it returns 0 (therefore the email is forwarded to a bounce back address). If not, the email ends up being processed as usuals (in my case, to ./Maildir). I do this because I regularly send out to a mailing list, and typically get bombarded with bouncebacks to the address I'm sending from. with the php file, I ideally wanted to do two things. 1. return a code such that qmail forwards the message to a general bounceback address, and two, log the recipients email in a database, and automatically exclude it from future mailings.

Everything seems to work except for the fact that condredirect doesn't care what is returned from my php file (0 or 99). It always redirects the email to bounceback. I also striped my php file to test this theory:

#!/usr/local/bin/php

and

#!/usr/local/bin/php

...both have the same result.

I guess the way around this is to not use conredirect at all, and have the php file do all the work (i.e. repackage the email and send it using the headers/email body with Pear_Mail or something). I just think using conredirect is a more elegant solution....if it works with php. I wonder if it has something to do with the way php returns codes.

login or register to post comments

woooops

Submitted by PIlias on February 2, 2007 - 19:35.

I just answered my own question. If you want the above to work, use exit in php, not return.

so, when testing the email, if you want qmail to redirect it, follow the above, but make sure you have this line in the php file:

exit(0);

and it will be redirected. otherwise, use exit(99), and it will be processed as usuals.

nice. it's good to talk things out :)

login or register to post comments

Message not fully parsed

Submitted by JamesWC on March 13, 2007 - 20:35.

I have the same problem as steelo - can anyone please offer a solution?

login or register to post comments

permission problem?

Submitted by fishnyc22 on May 15, 2007 - 05:50.

I've been trying to get this going for a while. Can't seem to get it right. I've tried removing the shebang an moving it into the .qmail but No luck. With this in qmail:
|/etc/bin/php -q |/var/www/vhosts/domain.com/httpdocs/_phpcgi/test.php
or
|/etc/bin/php |/var/www/vhosts/domain.com/httpdocs/_phpcgi/test.php
and my php file simply as:
<?php
// Send
mail('test@gmail.com','test subject', 'my message goes here');
?>
I'm getting this error: May 15 01:25:29 as qmail: 1179206729.939189 delivery 2: deferral: No_input_file_specified./


When i try and switch it around: .qmail looks like this
|/var/www/vhosts/domain.com/httpdocs/_phpcgi/test.php
php file looks like this
#!/usr/bin/php -q
<?php
// Send
mail('test@gmail.com','test subject', 'my message goes here');
?>
When set up like this my error is: May 15 01:37:36 as qmail: 1179207456.473053 delivery 1: deferral: /bin/sh:_/var/www/vhosts/domain.com/httpdocs/_phpcgi/test.php:_Permission_denied/

I'm running plesk 8.1 on Fedora, My permissions are as follows:
-rwxr-xr-x 1 ftpuser psacln 98 May 15 01:37 test.php

and for qmail:
-r-------- 1 popuser popuser 73 May 15 01:36 .qmail

Any help is really appreciated. Is this the latest method or is there some other way to get this going?

login or register to post comments

hello

Submitted by anandp_9 on July 27, 2007 - 08:19.

Hai I am anand, at last I found this article to trace inmail in php. But now how can i get pure text from the script. I need to get only from,subject and message. Anybody help me out. Thanks in advance. aNaNd KuMAr.

login or register to post comments

Mail delivery notification

Submitted by hexburner on September 28, 2007 - 13:06.

Many people here have problems with mail delivery notifications, and so had I.

I'm using EXIM, and PHP as an Apache module. In cPanel I had the option to add a forwarder using a pipe to a script. At first I got delivery notifications that the e-mails sent to the e-mail address using the script could not be delivered.

This works for me:

  1. First line in the script: #!/usr/bin/php -q
  2. Second line, without spaces, open the PHP-tag: <?php
  3. At the end of the script: return true; instead of return NULL;
  4. Last line, without spaces, close the PHP-tag: ?>

I use MIME Parser to parse the e-mail.

login or register to post comments

Got it working, but...

Submitted by bmwall on October 15, 2007 - 21:44.

hexburner, I got it working, following your instructions, but messages come out all run together (not separated by mime type for a multi part message).

Can you post the Mime Parser code you use to forward the message.

It sounds like we are doing a similar thing. I want someone to be able to send an email, using their email address, given by my site, then when the receiver hits reply, it should go to the PIPE program. Then I want the program to reprocess the message, and send it out, to my user, at his existing email address.

I got that all working, except when the email is multi part.

Any help there would be appreciated.

thanks!

login or register to post comments

loop problem

Submitted by jeboy25 on November 26, 2007 - 02:02.

Hi! I created a bounced email management system. All bounced email are returned to a specified email and from this email it will be redirected to a script that will parse the bounced email so that I can be able to get the recipients address and the reason why it is bounced. It perfectly redirected to my script but the problem is when I loop over it, it doesn't store to a variable ($strReason). Here's the code:

// read from stdin $fd = fopen("php://stdin", "r"); $email = "";

while (!feof($fd)) { $email .= fread($fd, 1024); } fclose($fd);

// handle email $lines = explode("\n", $email); $strReason = "";

for ($x = 0; $x < count($lines); $x++){ $strReason .= $lines[$x]; }

// Store bounced email to database $sql = "INSERT INTO tbl_bounced_email(Reason) VALUES('$strReason')"; $r = mysql_query($sql, $link) or die (mysql_error().' '.$sql);

but when i set the loop to 16 only, it stores the message to $strReason for ($x = 0; $x < 16; $x++){ $strReason .= $lines[$x]; }

login or register to post comments

404

Submitted by stevenmc on December 10, 2007 - 08:59.

http://gvtulder.f2o.org/evolt/mail/mail.php.txt = 404

I've spent ages trying to make this work. My symptoms are very similar to fishnyc22 above. My .qmail file is located in...

/var/qmail/mailnames/myDomainName.com/EmailUserName

I'm running Apache 2, PHP 5 as CLI, Qmail. It's a 1&1 server, and I am wondering if they are imposing restrictions on me.

My Qmail file looks like this...

| true

./Maildir/

&myEmailAddress@gmail.com

|/usr/bin/php -q /var/www/vhosts/myDomainName.com/cgi-bin/test/emailtophp.php

And my PHP file looks like this (chmod 777 emailtophp.php):

< ?PHP $message = "Bla bla bla"; $message = wordwrap($message, 70); mail('myEmailAddress@gmail.com', 'My Subject', $message); exit(); ? >

(Spaces in php declaration due to technical restrictions on this site) I get returned emails saying...

Hi. This is the qmail-send program at myServerName. I'm afraid I wasn't able to deliver your message to the following addresses. This is a permanent error; I've given up. Sorry it didn't work out. myDomainName.com>: preline: fatal: unable to run /var/www/vhosts/myDomainName.com/cgi-bin/test/emailtophp.php: access denied

It's the "access denied" bit I'm annoyed about. I've tried all I can. Running the script (without the pipe) directly from the command line works, but I can't get the file to execute when a mail has been sent to it. Gurr!!!

login or register to post comments

Figured it out - Qmail Working

Submitted by stevenmc on December 12, 2007 - 14:16.

Ok, I figured out what the problem was. Permissions! Yes, I did chmod 777 of the file (777 is dangerous, but good for debugging), but the cgi-bin (two levels beneath) didn't have the right permissions. So I changed the php file to be in a new purpose made directory under myDomainName.com called phpcgi (because I'm running php as cgi).

Then I ran:

chgrp popuser phpcgi

chown popuser phpcgi

chmod 777 phpcgi

And done the same for the file as for the directory.

It all works fine now! Phew.

login or register to post comments

access denied error

Submitted by agtest on December 14, 2007 - 12:44.

I too have the same access denied problem. When I send mail to username@domain.com I got the following error: The following text was generated during the delivery attempt: ------ pipe to |/usr/bin/php /home/stempag/public_html/phpcgi/testrun.php generated by stempag@egdomain.com (ultimately generated from stempag@egdomain.com) ------ Status: 403 X-Powered-By: PHP/5.2.4 Content-type: text/html Access denied. I have followed the steps as above. I have changed the group and owner of phpcgi folder to mailman. My doubt is this setting could be wrong. To which user should I change to. Help pls. Thanks in advance.

login or register to post comments

What I think

Submitted by stevenmc on December 14, 2007 - 13:32.

Hey AgTest. I can only suggest things with Qmail, as this is the limit of my experience. Ok, first I would ask why chose mailman as the user. Navigate to your Qmail folder for your "username"
locate usernameForMyEmailAddress
This will give you the location of your username folder for Qmail (hopefully). Type:
ls -al
What's the username in the list for your .qmail file? chown and chgrp of your PHP file to that username.

Additionally, move "phpcgi" folder to one lower level, so it's path is...

/usr/bin/php /home/stempag/phpcgi/testrun.php
Then chmod to 777 (or 755 if you prefer).

Why?

Because moving the folder here will allow you to chmod to 777 more safely. Finally, update your pipe...

|/usr/bin/php /home/stempag/phpcgi/testrun.php
Try that!

login or register to post comments

MIME Parser

Submitted by chj on December 15, 2007 - 15:50.

I am also looking at getting a forward script up and running. Can anyone share how they parse the e-mail and have it forwarded? Thanks

login or register to post comments

Access denied solved but mail delivery failed

Submitted by agtest on December 17, 2007 - 13:34.

Hi stevenmc, Thanks for your help. I located the user of the mail program. In my case it is mail and I changed the owner and group to mail. Now the access denied problem is solved. :-). But I am getting Mail delivery Failed message with the output generated by the php program to which it is piped. In the php program I am echoing the received mail content. Pls suggest me. Thanks.

login or register to post comments

Hm

Submitted by stevenmc on December 24, 2007 - 15:12.

At this stage agtest, I would suggest you run your php file directly from the command line and see if your program sends an email successfully. If it doesn't, either you haven't specified the #!/usr/bin/php -q line properly, or you have a syntax error.

login or register to post comments

Issues with Plesk & Piping

Submitted by sicsol on December 30, 2007 - 06:59.

I've been ripping my hair out trying to figure out this issue.

I'm using the following:
Plesk 8.2 on linux system

bounces@domain is the email i'm setting up

I've edited the .qmail file located in /var/qmail/mailnames/domain/bounces/ with the following

|/usr/bin/php -q /path to php file/
./Maildir/

I've chmod 755 the php file.

The beginning of the php file has the following

#!/usr/bin/php -q
<?php
// read from stdin
$email = "";
$fp = fopen("php://stdin", "r");
while(!feof($fp)) $email .= fgets($fp, 4096);
fclose($fp);

After that i'm inputing the $email value into a database.

In order to test the system i'm sening an email to bounces@domain and i get a returned email with failure, and the information isn't submitted into the database.

My reply is as follows

bounces@domain
No Input File Specified.

can anyone help with this issue.

login or register to post comments

Email2HTTP for handling incoming email

Submitted by tt on January 14, 2008 - 00:11.

Hey, if you're interested in an alternative/easy way of handling incoming email in your PHP script, I would like to invite you to try out Email2HTTP. Contact me if you have any questions, I would be more than happy to help.

login or register to post comments

email2php.com

Submitted by email2php.com on January 14, 2008 - 11:47.

Hello, that's where we at email2php.com also want to join in. Our service is up and running since November 2007, and meanwhile serves some (still modest) 10-15 emails a day. You are invited to try.

login or register to post comments

Hosted by a company

Submitted by jeherron on January 28, 2008 - 18:11.

Is there a way to do this if you site is hosted at godaddy

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.