SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fetching mail from alias@domain.com to a PHP script

    I won't babble too much, right to the point.

    How can I get all e-mails that are sent to something@mydomain.com to run through a PHP script, that can use the e-mail contents and insert them into a database (MySQL), etc.

    And please be gentle as I am a complete newbie when it comes to SMTP, POP3 or whatever is needed here. The biggest thing I've done with PHP and e-mail starts and ends with mail().

    Thank you!

  2. #2
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it can get complicated..
    I suggest you pipe all email for that user into a php script that parses the email and dumps it into the database..
    here's an entry I have in my /etc/aliases file:

    pipe: "|/home/sean/piper"

    and here's /home/sean/piper:

    PHP Code:
    #!/usr/bin/php -q
    <?php

    $stdin 
    file("php://stdin");

    $fp fopen("/var/www/virtuawin/list.txt""a");

    foreach(
    $stdin AS $ln) {
        
    fwrite($fp$ln);
    }

    fwrite($fp"-----------------------------\n");

    fclose($fp);

    ?>
    all email sent to pipe@caedmon.net get logged into /var/www/virtuawin/list.txt (http://virtuawin.caedmon.net/list.txt)

    S

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firefly, you could look in reply.php and reply_pop.php in DeskPRO. I guess that was what the ICQ message was about

  4. #4
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris - that's where I first looked, but I couldn't understand anything (except for the putting it in database part )).

    scoates - thank you very much, I will toy with that code for a while.

    While I'm at it, I have one more thing on my mind. Currently my e-mail is set up, so that everything-here@domain.com goes to chen@domain.com. In Outlook, I can clearly see what the original To: was (for example, if someone sends firefly@domain.com I will see that address although I get it through chen@domain.com). I'm just wondering if this is also the way PHP "sees" it? This is critical for me.

    Thank you both.

  5. #5
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Getting the to: can be done a few ways. You can use the IMAP functions if you decide to collect the mail via IMAP but if you want to use the pipe method of getting the email you can't do this I don't believe.

    You would need to parse the email, you have the email in a variable and just have to run through it. You can see how it is done for deskpro using that MimeDecode.php file

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, if you pipe the email to a program, you will have to read the contents of the email in from stdin and parse it as text.

    Eg;

    $email_as_arry_of_lines = file("php://stdin");

    will read the email from stdin into an array.

    Don't forget that you can pop the pipe-to-program redirection rule in a .forward file in the user's home directory too (if you don't have access to the alias file of your email server).

    If you use postfix as your mail server you can configure it to automatically deliver all email into a mysql or postgresql database. Neat mail server is postfix.

  7. #7
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I confused you guys a bit with my last post.

    I was just wondering, will the To: line in php://stdin say chen@domain.com (the 'real' address), or firefly@domain.com (the 'fake' address, that the sender used).

    I'm also going through the file that scoates showed and I think I can see a pattern, so it can't be hard to get the info I need from that array of lines.

    Thanks.

  8. #8
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THe To header should be the email address the sender sent the email to (your alias I presume).

  9. #9
    Custom User Title v1.0 FireFly's Avatar
    Join Date
    Aug 2001
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freakysid
    THe To header should be the email address the sender sent the email to (your alias I presume).
    Looks like I'm gonna find out soon enough.

    Thanks for all the help.

  10. #10
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by scoates
    it can get complicated..
    I suggest you pipe all email for that user into a php script that parses the email and dumps it into the database..
    here's an entry I have in my /etc/aliases file:

    pipe: "|/home/sean/piper"

    and here's /home/sean/piper:

    PHP Code:
    #!/usr/bin/php -q
    <?php

    $stdin 
    file("php://stdin");

    $fp fopen("/var/www/virtuawin/list.txt""a");

    foreach(
    $stdin AS $ln) {
        
    fwrite($fp$ln);
    }

    fwrite($fp"-----------------------------\n");

    fclose($fp);

    ?>
    all email sent to pipe@caedmon.net get logged into /var/www/virtuawin/list.txt (http://virtuawin.caedmon.net/list.txt)

    S
    what would i do if i didn't have access to the /etc/aliases/ file?
    Nearly 7 years old!

  11. #11
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could use your /home/[user]/.forward file.

    add this line:
    [user], "|/path/to/script.php"

    where user is the username you'd like to capture..

    sendmail might have issues here, though.. it sometimes needs to have piped executables privileged .. it gets messy, as I said..

    S

  12. #12
    SitePoint Evangelist Umair.ms's Avatar
    Join Date
    Sep 2000
    Location
    Lahore, Pakistan
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Write a script that check emails from a pop3 account. And then schedule it using (dont remember the name of that utility) to run that script each 5 or 10 mins or whatever you like.
    TinyPlanet.org
    Discuss and Debate World Events, Politics and Religion.
    Interact and Share your Views with People around the Globe.

  13. #13
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah.. 'cron'
    is the name of that utility..

    with the pipe method, though, you don't even need said account to actually exist (if you have access to /etc/aliases).. pipe@caedmon.net is not a real user..

    S

  14. #14
    SitePoint Evangelist Umair.ms's Avatar
    Join Date
    Sep 2000
    Location
    Lahore, Pakistan
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried that piping method (at that time I found a lot of stuff about it on the web). But, still have no idea why it didn't work for me.
    TinyPlanet.org
    Discuss and Debate World Events, Politics and Religion.
    Interact and Share your Views with People around the Globe.

  15. #15
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah.. like I said.. it's complicated..

    then again, pop3/imap can also be a wee bit complicated (-;

    either way would work fine, and both ways have their own ups and downs..

    S


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •