0

I have a PHP website in a managed server (I only have access through the cPanel), and I would like to execute something when an email is received, something like adding the email address and the email content to a database.

I know how to do it with GAE and in ASP, but I can't find how to do it in PHP.

Community
  • 1
  • 1
stenci
  • 8,290
  • 14
  • 64
  • 104

1 Answers1

1

There are two ways to accomplish what you want:

1 - Make a script that checks a email at regular intervals and do whatever you want with the content of the email.

2 - Redirect a email account to an email processor and once again, do whatever you want with the content of the email.

As you are using cpanel the you need the to create a forwarder (email alias) and set it to the full path of the script you create to process the email with the pipe symbol | like this:

|/full/path/to/emailHandler.php
Babblo
  • 794
  • 5
  • 17
  • Thanks! But when I send an email I receive this: `/home/stefanom/public_html/pool/unsubscribe.php: line 1: ?php : No such file or directory`. If I add the line `#!/usr/bin/php` it says it can't execute the file. On the cPanel I found `/usr/bin/perl` but I didn't find the php path. Any idea? – stenci Dec 09 '13 at 05:07
  • First make sure you have PHP CLI compiled, then PHP by default is installed in /usr/bin or /usr/local/bin. – Babblo Dec 09 '13 at 12:50
  • Now the script runs and creates a test file (just to show me that it did the job), but the sender receives an email from `Mailer-Daemon@sti.stidev.com` saying `this is a permanent error` followed by the text of the message. In the text there is no mention to the error. – stenci Dec 09 '13 at 15:28
  • Your script need to end returning 0 (zero) to the mail server. – Babblo Dec 09 '13 at 15:29
  • If the script ends with `exit("this is a text");\n?>` the email contains the text followed by `------ This is a copy of the message, including all the headers. ------\n...`. If it ends with `exit`, `exit(0)` or `exit(1)` the email doesn't contain the number, but it still arrives with the same content. Is there another way to return zero? – stenci Dec 09 '13 at 15:41