skip to Main Content

I have a PHP app that I built using the Slim routing framework. The app needs to send dynamic emails from orders so that users can just respond to those emails and the response goes right back into the app (stored in MySQL). I can easily create the dynamic address per order and that goes out just fine. My problem is getting it back.

I setup a subdomain (mailer.example.com) and in cPanel I setup a forwarder to catch all mail to that subdomain and forward it to a specific PHP file. That php file reads stdin and grabs the mime message and currently writes it to a file:

#!/usr/bin/php -q
<?php
// read from stdin
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd))
{
    $email .= fread($fd, 1024);
}
fclose($fd);

$filename = "mail_".date("mdYHis").rand(1,99999999);
file_put_contents("mailfiles/".$filename, $email);

header("Location: http://www.example.com/public/mailer/process/".$filename);

As you can see, at the end I would like to forward this to my actual app which has all the database calls and other routines to process the email. But I don’t know how to get the request into the app. It seems to ignore the header call above.

Am I doing this all wrong or should I just do all the processing I need in this script? I realize that would maybe be the easiest path forward but I’m also trying to use a mail parse library that fits nicely in my app.

Not sure if that all makes sense. Let me know what other info you need.

2

Answers


  1. I think what you’re looking to do isn’t to return an HTTP response with a Location header, but rather initiate an HTTP request to your web server.

    In that case, you should replace your header() call with:

    $ch = curl_init('http://localhost/public/mailer/process/' . $filename);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    

    Note that your script (the one called to save the mail contents) will wait for your app to finish processing the request. If this is an issue for you, you’ll need to use a technique to run PHP processes in the background.

    Login or Signup to reply.
  2. You can read a mailbox with php and parse the emails that way.

    // To connect to a POP3 server on port 110 on the local server, use:
    $mbox = imap_open ("{localhost:110/pop3}INBOX", "user_id", "password");
    

    I would not, create a custom reply-email/mailbox, per order, instead add the order to the subject as a reference. That way you only need to parse one mailbox.

    Find the imap functionality here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search