tags:

views:

46

answers:

5

Hello,

I want to update a blog, by sending an email using php. This is not wordpress, tumblr, or the famous ones.

I could set up an email id, with a specific code in the subject line, but how I get input the contents into the db?

Thanks Jean

A: 

You probably need to do socket programming to listen to the email incoming port and whenever you receive an email via the port (make sure you comply with the correct protocols on the higher layers) , you can take in the body and subject and post it into your blog's database.

You will need to keep the script running in a loop. However, this kind of task are usually not done on a platform like PHP. You can do console based applications to run on your server on C or C++ to perform such tasks as they are more suitable.

Dedicate PHP to serve the front end of your website/server.

thephpdeveloper
what exactly would trigger the script to listen to an incoming mail.
Jean
you will need to keep the script running in a loop. However, this kind of task are usually not done on a platform like PHP
thephpdeveloper
Would it not increase server load?
Jean
A: 

You can use a job scheduler like cron to periodically check for new emails and update the database.

Or use a web service like webcron http://www.webcron.org/ to launch your script, similar to cron.

zaf
Would that work if the hosting and mail server are at 2 different locations?
Jean
Yep! You can retrieve emails from a remote server - even gmail!
zaf
+2  A: 

You could set up a cronjob starting every x minutes and checking for new mail. Use the PHP imap-functions to retrieve and parse the mail and then dump the contents into your database.

http://php.net/manual/de/book.imap.php

Select0r
If the mail server supports IMAP ;)
Felix Kling
Well ... yes ... ;) If not, there are PHP classes out there to handle POP3-access via PHP.
Select0r
A: 

You'll have to use PHP to routinely check the email account via something like POP3. This blog post talks about doing exactly this

used2could
+1  A: 

As others have suggested you could poll a mailbox to detect an incoming message - but its far more efficient to get the MDA (mail delivery agent) to trigger the process when a mesage arrives - how you do this depends on how your mail system is configured (on most Linux systems procmail is used for the MDA which provides all sorts of filtering, automatic responses and execution functionality, failing that, .forward files in the users home dir can usually do at least part of the job).

In terms of parsing the message - it should contain one or more headers seperated by a newline (or carriage return + newline) followed by at least one blank line, followed by the body. But you'll need to think about how you deal with mime encoded content.

Regardless of how you get the email you still need to get it into the blog.

Most blog software supports some form of xmlrpc for adding messages - there's a lib published at http://phpxmlrpc.sourceforge.net/

Have a look at the discussion pages for examples of usage as a client.

Failing that - try reverse engineering the form used to submit a new post.

C.

symcbean