Handle Incoming Mail with PHP script

Dari tulisan Gijs van Tulde, hasil pencarian lewat Google.com keyword "Handle Incoming Mail with PHP script", sangatlah menarik.
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...
Dan tentunya saya hanya melengkapi sedikit sekali dari artikel Gijs van Tulde, sangat jelas yang ditulis dalam artikel step by step nya, bahkan jika anda membaca bagian commentnya disitu banyak yang mendiskusikan tentang masalah yang terjadi dan bagaimana mengatasinya. Berikut adalah Script php dari artikel Gijs van Tulde, Step 3:

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

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 < subject =" $matches[1];" from =" $matches[1];" splittingheaders =" false;" style="font-weight: bold; color: rgb(255, 0, 0);">?>

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.

Dari tulisannya saya coba satu persatu, mulai dari step 3. Mengapa Step 3 ?? ya karena sebelumnya server sudah terinstall sendmail, kebetulan menggunakan share hosting :) (Webhosting) jadi tidak perlu repot. Dari script diatas saya upload ke webhosting (sesuai dengan petunjuk webhosting mengenai pine email dengan script php) biasanya di folder home dan file php di ubah ke mode 755. Lalu saya buatkan satu email forwader yang di forward ke script php tadi. Saat di coba kirim email ke alamat forwader tersebut berjalan dengan baik, hasil parsing saya simpan ke dalam file email.txt. Tapi beberapa saat kemudian muncul pesan balasan dari server ke email saya seperti berikut : Mail delivery failed: returning message to sender. Ada apa ya ?? Akhirnya dapat sedikit informasi (lupa alamat referensinya) bahwa sendmail saat menerima email dan selesai membaca diperlukan nilai balik true (return true). Akhirnya dibagian bawah baris sebelum tanda ?> saya tambahkan return true. Dan BERHASIL... berikut contoh scriptnya: #!/usr/bin/php -q <?php // read from stdin $fd = fopen("php://stdin", "r"); $email = ""; while (!feof($fd)) { $email .= fread($fd, 1024); } fclose($fd); // handle email $lines = explode("\n", $email); // empty vars $from = ""; $subject = ""; $headers = ""; $message = ""; $splittingheaders = true; 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; } } $fp=fopen("mail.txt","w+"); fwrite($fp,"Headers\n$headers\nJudul:\n$subject\nDari:\n$from\nPesan:\n$message"); fclose($fp); return true; ?> Selanjutnya silahkan berimajinasi sendiri untuk pengembangan script ini.

Komentar