Welcome Guest, Not a member yet? Register   Sign In
How to get an email to trigger an HTTP request (or otherwise execute some function in PHP)
#1

[eluser]aidehua[/eluser]
I am developing an application that allows users to contribute content to a website by email.

My question is: how best to get the email to call an HTTP request (or otherwise execute some PHP function on the webserver)?

For an example of this working in practice, see the Blogger blogging platform. You can send an email to a designated email address, and Blogger will convert that email into a post on your blog.

The obvious way to go would be to use something like the pop3class on http://www.phpclasses.org/browse/package/2.html

This gives your PHP script access to the contents a POP3 server. No problem then, in principle, to get the email contents and add it to your database or do whatever you need with it.

But when do you run the script? You could say "on demand" - whenever a user wants to view the data, the script runs to check for new messages on the POP3 server. But if the POP3 server is slow to respond, that could give unacceptably slow page load times.

Alternatively, you could run the script as a cron job every n seconds, avoiding the need to query the POP3 server "on demand" while the user is waiting.

But both these approaches rely on relatively inefficient "polling" of the mailserver, whether or not there is a new message waiting.

(For this particular system, we envisage ~1 message/hour, rather than one message per second: it seems inefficient to be polling every 10 seconds for a once-in-3600-seconds event; but I don't want to poll only once every 10 minutes because that would create a delay of up to 10 minutes before new messages appear.)

Does anyone

(a) have any comments or suggestions on the above approaches

or

(b) know of a "push" approach, that would enable an email to be "pushed" to the webserver (perhaps as an HTTP POST request?) as soon as it arrives on the mailserver?

Grateful for any ideas.

Thanks,

Ed
#2

[eluser]ggoforth[/eluser]
On my web server which uses cPanel, I have the option (under mail forwarders) to "Pipe to existing program". This "pipes" the email to a specific PHP script that will need to parse the email and then do something with the pieces. I think this would be your best bet, as it doesn't involve polling. It will all happen as soon as the email hits the server and the server pipes the email to the waiting PHP script.

Greg
#3

[eluser]BrianDHall[/eluser]
[quote author="ggoforth" date="1259126035"]On my web server which uses cPanel, I have the option (under mail forwarders) to "Pipe to existing program". This "pipes" the email to a specific PHP script that will need to parse the email and then do something with the pieces. I think this would be your best bet, as it doesn't involve polling. It will all happen as soon as the email hits the server and the server pipes the email to the waiting PHP script.

Greg[/quote]

Cheers to you sir! I was wondering about such functionality as well, and this solves it! On CPanel it is "Forwarders" under the Mail heading.

This could be used for receiving input via SMS, as most people can send a text message to an email address for the same price as a regular text (zero if you have an unlimited plan).

Very handy!
#4

[eluser]renownedmedia[/eluser]
Does anyone know of a push technique that doesn't require the forwarder being predefined? E.g. How to create a webmail app using php?
#5

[eluser]BrianDHall[/eluser]
[quote author="Thomas Hunter" date="1259176705"]Does anyone know of a push technique that doesn't require the forwarder being predefined? E.g. How to create a webmail app using php?[/quote]

Sure, apparently this is a feature that is entirely dependent on your servers sendmail setup, so you need either root access or an accommodating hosting provider.

http://www.linuxquestions.org/questions/...ved-55835/

http://www.techniqal.com/blog/2005/11/14...rom-email/

This doesn't seem to be very commonly used so I didn't find anything specific to PHP, but you don't really need it.

All you need to figure out is how to get your sendmail program to activate a script upon email reception. Then you can just use the cron-related tutorials on running CI from command line, and off you go!

EDIT: Also you can probably use some PHP classes out there to control mailbox creation, so you can automate the creation of the mailbox and therefore control the piping, too, if you only want certain mailboxes to get handled instead of getting all emails to trigger the result.
#6

[eluser]sophistry[/eluser]
if i am reading this discussion properly, the only thing that is at issue is which piece of software is doing the "polling".

if you use a special configuration of sendmail to pass emails to a script you are using sendmail to "poll" for emails.

if you use a POP3 or IMAP library with cron (more portable because it is more widely supported IMO) then cron is doing the "polling" for emails.

what is the practical difference other than the "feeling" that you are over-polling the mailserver?

how often does your email client poll the mailserver? mine is set at 5 minute intervals. is that a waste of resources?

oh, and check out my imap_pop library (the POP3 lib on PHPscripts is pretty rudimentary) in the wiki. (link in sig).

cheers.
#7

[eluser]swissbastian[/eluser]
Hi there. I've got an mail address which forwards messages to a PHP script. For that script I'd like to read and save all incoming messages. But how do I read them with PHP? Thanks for advices.




Theme © iAndrew 2016 - Forum software by © MyBB