How can I make it so when I e-mail for e.g. news@example.com text/information of the e-mail is written into a mysql table? It would extract the contents of a new email and write them into MySQL table. This table manages news articles on the website, so in the MySQL columns:

"content" is the body of the message

"caption" is the title of the news article

"snippet" is the title that will appear in a "Latest News" block


"date" is the date the article is posted.


$subject = caption

= uri

$message = content

= snippet(first two lines of content)

$date = date (in Unix timestamp)

The table is named bx_news_entries manages the news articles visibly.

I researched and saw a user who posted this on stackoverflow who's code looked like so:


$imap = imap_open("{gmail.com}", "username", "password");

if( $imap ) {

     //Check no.of.msgs

     $num = imap_num_msg($imap)

     //if there is a message in your inbox

     if( $num >0 ) {

          //read that mail recently arrived

          echo imap_qprint(imap_body($imap, $num));


     //close the stream




We are using an exchange server..I am a coop student so I am not really advanced at this.

I tried this as a test to see if it works, logging in gmail to read email. It didnt work.


// connect to the mailbox

$m_mail = imap_open("{mail.https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2}INBOX", "username", "password");

//get all messages

$m_search=imap_search ($m_mail, 'ALL ');

// Order results starting from newest message


//loop through and do what's necessary

foreach ($m_search as $onem) {

    //get imap header info for obj thang

    $headers = imap_headerinfo($m_mail, $onem);

    $head = imap_fetchheader($m_mail, $headers->Msgno);

    $body = imap_body($m_mail, $headers->Msgno, FT_INTERNAL );

  echo $body;


//purge messages (if necessary)


//close mailbox



I have the code imagined in my head, i just need to bring it to fruition.

Can somebody assist me with this please?

5 Years
Discussion Span
Last Post by fobos

See if you can connect using this?

/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'someone@gmail.com';
$password = 'yourpassword';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

See if you can connect using this?

/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'someone@gmail.com';
$password = 'yourpassword';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

Which parts do you need help with? I have a better mail checking script at home I can post later that will handle different encodings, multiple email parts, attachments, etc.

It is not a g-mail account, it is a private e-mail on my server. Though I don't know how to find out the imap host for it in cpanel.

This is how it the news block looks:

Lets say the e-mail is aligned to like this on particular lines so the script would know which line would go into which column in the table.

From: admin@example.com

To: newsfeed@example.com

Subject: Test News Article! <--- This would serve as the caption and uri for the news article, and would go into the caption column and uri column.

test news article <--- (Line 1:) This line would be for the tags that get inserted into the tags column

12/20/2011 <--- (Line 2:) This line would go into the date column but would have to be converted to Unix timestamp

(Line 3, based on line breaks,) Would be the message that gets inserted into the content column, and the first line only would automatically be inserted into the snippet column

<p>Hello. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

How could i attain this?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.