954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get contents from e-mail and write it to MySQL table

How can I make it so when I e-mail for e.g. [email]news@example.com[/email] 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

"uri"

"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:

<?php

$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

     imap_close($imap);

}

?>

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.

<?php

// 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

rsort($m_search);

//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)

imap_expunge($m_mail);

//close mailbox

imap_close($m_mail);

?>


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

Can somebody assist me with this please?

IanArcher
Light Poster
29 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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());
fobos
Posting Whiz in Training
297 posts since Feb 2009
Reputation Points: 29
Solved Threads: 52
 

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());

[quote name='justsomeguy' timestamp='1324408390' post='228791']

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.[/quote]

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:
http://i44.tinypic.com/2rwuo1u.jpg

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: [email]admin@example.com[/email]

To: [email]newsfeed@example.com[/email]

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

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.

How could i attain this?

IanArcher
Light Poster
29 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

are you using outlook? if so use the smtp code to connect

fobos
Posting Whiz in Training
297 posts since Feb 2009
Reputation Points: 29
Solved Threads: 52
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: