www.webdeveloper.com
Results 1 to 12 of 12

Thread: Here's a challenging one - having a PHP-based website accept incoming E-mail

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62

    Question Here's a challenging one - having a PHP-based website accept incoming E-mail

    Hi, everyone,

    Various bulkmail related subjects have been discussed here, but none address the problem that my client has dropped into my lap this morning. So here goes. :-)

    It is fairly routine to have a chunk of PHP-based website code send out an E-mail message (HTML-formatted or otherwise) to each recipient in a list (either retrieved from a mySQL table, a flat file, or whatever) and handle all related tasks.

    What I am looking for, though, is a way to have a website forward an incoming e-mail to all recipients on a distribution list - have the PHP website act as a mailing list engine, in other words.

    Authentication and control could be done on the basis of passwords in the subject line, various custom header fields, and what not. That's not the hard part.

    The hard part (and the central question in this post) is how to get the e-mail into the website? My client would like to create an e-mail message using their standard e-mail applications (be it Outlook, Gmail or whatever) and "send that e-mail to the website" (to use their words) which must then take care of further distribution.

    So... how am I going to "send an email to the website"? Which will of course typically be hosted in a shared hosting environment, so we won't have access to the server in order to set up facilities that deal with incoming e-mail, a mail server or something similar.

    Having a PHP-based chunk of website code accept an incoming POP3 (or IMAP) session would be the key to solving this particular puzzle, I believe. MX records etc. to get it to the server would be dealt with at the DNS level, which is solvable...

    But how am I going to make the website accept incoming E-mail? Can this be done at all? And if so, how?

    Maybe I could open a socket that listens on a given port for incoming connections? (There would be firewalling issues to be discussed with the hosting provider, of course.) If it is impossible to directly accept incoming e-mail, a second option (as a work-around) could be to send the "incoming" e-mail to a dedicated external mailbox, and have the website poll this periodically to collect it... But that creates a new problem in how to do schedule this, without access to cron. I know how to fork off a child process that then continues to run as a daemon in the background, so presumably I could do this on the first page load and then have each subsequent page load check for the presence of the daemon process in the background, but this does seem a rather convoluted and problem-prone approach to me... not to mention the fact that the long-term survival of the daemon process is not guaranteed, given the fact that I know nothing about what goes on at the server OS level. But it's either that, or add another step to the process flow: create the e-mail, send it off to the dedicated e-mail address, and then (after allowing time for delivery) start the fetch-and-distribute cycle manually via a form on the website... which I find less elegant, not to mention more cumbersome (and therefore error-prone) to the typical secretarial employee who will most likely be charged with this job.

    Anyway. Food for headaches - in plenty! :-)

    All suggestions, hints, pointers and donations of aspirin will be greatly appreciated!

    // FvW

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780

    Cool

    Mail transport is processed using a different protocol and port mapping than web based HTTP. Thus, a web site cannot "directly" receive email and if it could, you'd have issues with spam, security and DDOS. This is not to say you can't create mail bots that tie into your local or a remote MTA or other read inbound mail on the server and interact with it (POP or IMAP) such as forwarding, etc. Think Google Mail without the fancy front end.

    Great scripts related to PHP and advanced mail handling:

    http://www.gotapi.com/php/php_mail_tutorials.html

    It is also technically possible for PHP to process inbound email on the actual server but you still need an MTA, the PHP must be compiled as CLI (not as an Apache Module) and you'll need shell access to the server to make some changes.

    Details here on that:

    http://www.evolt.org/incoming_mail_and_php

    I also wish to point out that this latter technique is similar to how clamav (anti-virus) and procmail are integrated into an MTA, it's nothing more than a PHP script being called during inbound mail processing. What the script does, i.e. generate files to be imported into a web site, forward email, whatever, is up to you.

    -jim
    Jim, Sr. Web Developer
    You know who you real friends are when you ask them to move your furniture or paint.

  3. #3
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62
    Hi, Jim,
    Quote Originally Posted by SrWebDeveloper View Post
    Mail transport is processed using a different protocol and port mapping than web based HTTP. Thus, a web site cannot "directly" receive email and if it could, you'd have issues with spam, security and DDOS.
    I hear what you say. However I'm not convinced that the fact that HTTP is the primary protocol used by the web service is all that relevant. Using PHP sockets one can create a PHP3 or IMAP client, which is an example of using a protocol that has nothing to do with HTTP. If you can build a client process using sockets, why not a service? Isn't that what socket_listen() is for?

    I agree that security is an important issue. Especially the fact that firewalls and the like will probably stop incoming connections on other ports than 80 is something that I would have to discuss with the hosting provider. That said, any service (including HTTP) is potentially vulnerable to attacks and has to be secured properly. It's not like a server that only offers a HTTP service is not vulnerable while adding a POP3 service renders it insecure. Each service has its own requirements in security management, and adding POP3 as a service to a web site is no different.

    Quote Originally Posted by SrWebDeveloper View Post
    This is not to say you can't create mail bots that tie into your local or a remote MTA or other read inbound mail on the server and interact with it (POP or IMAP) such as forwarding, etc. Think Google Mail without the fancy front end.
    I'm not sure I follow you here. Could you elaborate?

    Quote Originally Posted by SrWebDeveloper View Post
    Great scripts related to PHP and advanced mail handling:
    Thanks - interesting reading indeed. However all of these (as far as I can see, having quickly gone through it) have to do with using PHP to create mail clients that send or fetch mail. My challenge is how to make a website accept incoming e-mail - in other words what I need is a service.

    Quote Originally Posted by SrWebDeveloper View Post
    It is also technically possible for PHP to process inbound email on the actual server but you still need an MTA, the PHP must be compiled as CLI (not as an Apache Module) and you'll need shell access to the server to make some changes.
    That's the thing - if I had shell access, I wouldn't have any problem and I'd be coding right now, rather than posting this and scratching my head. :-)

    I've also gone through the code of various Wordpress extensions and plug-ins (including gadgets that emulate cron and fetch mail from a dedicated external mailbox) but they're all user-initiated; a "real" cron job or a service that accepts incoming requests (implemented in PHP; I'm not counting the httpd, of course) is not something that I've been able to find.

    Thanks for the reply and helping me think, Jim! I appreciate it.

    // FvW

  4. #4
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    I just wanted to address the obvious security/firewall issues and also that interfacing with an existing MTA to process inbound mail was a common approach. As to code examples using sockets or otherwise, I could not locate any thus far and as you noted you don't have shell access, you're seeking alternatives. Disregard my other advice now that I know of that limitation.

    Check this out:

    http://www.hotscripts.com/listing/nameko/

    -jim
    Jim, Sr. Web Developer
    You know who you real friends are when you ask them to move your furniture or paint.

  5. #5
    Join Date
    Nov 2008
    Posts
    2,477
    I know this won't be an immediate help to you, but Building Scalable Websites has a chapter (chapter 6) dedicated to the "right" way to do this. I'd strongly recommend getting it if possible, it's a great book generally, and it covers receiving email in some detail.
    The first rule of Tautology Club is the first rule of Tautology Club.

  6. #6
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    I noticed the book is not language specific (i.e. not just PHP based).

    Can you confirm the technique used in the book means it can run standalone as an MTA and not rely on one?

    All the techniques or classes I've seen have PHP being used to retrieve and parse e-mail messages from a POP3 or IMAP using the PHP IMAP extension, i.e. a connection to a mail server (or local MTA) is involved.

    -jim
    Jim, Sr. Web Developer
    You know who you real friends are when you ask them to move your furniture or paint.

  7. #7
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by SrWebDeveloper View Post
    I noticed the book is not language specific (i.e. not just PHP based).

    Can you confirm the technique used in the book means it can run standalone as an MTA and not rely on one?

    All the techniques or classes I've seen have PHP being used to retrieve and parse e-mail messages from a POP3 or IMAP using the PHP IMAP extension, i.e. a connection to a mail server (or local MTA) is involved.

    -jim
    I don't remember the specifics unfortunately as I read it a while back, but I will have a look when I get to work tomorrow and let you know. I remember the example code is in PHP though which is why I mention it.
    The first rule of Tautology Club is the first rule of Tautology Club.

  8. #8
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Quote Originally Posted by Mindzai View Post
    I don't remember the specifics unfortunately as I read it a while back, but I will have a look when I get to work tomorrow and let you know. I remember the example code is in PHP though which is why I mention it.
    Cool, you know what happens when someone "assumes", they make an a$$ of our U or ME I only mentioned that because the book description didn't include anything about PHP.

    -jim
    Jim, Sr. Web Developer
    You know who you real friends are when you ask them to move your furniture or paint.

  9. #9
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    I'm struggling to remember what config file you'd need to edit ... but, I seem to recall being able to simply add a pipe to a users local mail config file, effectively allows mail for a particular user to have a forked delivery -- to a mailbox and/or a shell command (could be a php script).

    I'm not sure you'd need shell access to implement this. Though, I'm not sure this is the appropriate solution either ... It may be best to have your web app just check the mailbox as necessary via POP, IMAP, or direct file access. You'll avoid firing a new process for each message that comes in that way ...
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  10. #10
    Join Date
    Jan 2006
    Location
    South Africa
    Posts
    62
    Quote Originally Posted by Mindzai View Post
    I know this won't be an immediate help to you, but Building Scalable Websites has a chapter (chapter 6) dedicated to the "right" way to do this. I'd strongly recommend getting it if possible, it's a great book generally, and it covers receiving email in some detail.
    I would prefer to do this the "right" way, which would involve using a proper MTA to accept incoming e-mail - after all that's what it's for, and from a security and management standpoint this is how I'd want to do it if I could. However, I am limited to FTP access to a shared webhosting environment and I don't have shell access, let alone privileges to fiddle with sendmail or similar services on the server. So I'm forced to think about ways for "creative abuse". Hence this thread...

    // FvW

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles