如何使用PHP获取文本和图像格式

时间:2011-12-30 21:55:56

标签: php mysql

我有这个PHP代码,它使用IMAP函数连接到电子邮件帐户的收件箱,然后获取第一条消息然后获取该消息的正文并将其发布到管理新闻条目的MySQL表。

问题是我需要将格式化的文本和图像发布到表格中。例如,在包含消息的表的内容列中,我希望它具有粗体,斜体文本,图像位置以及它。但截至目前,它只获取电子邮件正文的原始文本。

我查看了imap http://www.php.net/manual/en/ref.imap.php的所有功能 但无法真正找到指定获取原始文本以外的电子邮件格式。

有人可以帮我找到解决方法吗?

<?php

    /** Strictly must use e-mail format (Script to find e-mail is case senstiive, so follow as seen below

        News Update                             
        Title: Article Title                                
        Tags: tag1 tag2             //do not insert commas, use lowercase only
        Categories: Article Category, 2nd Article Category
        Snippet: Article snippet.                       
        Message: 
        Article Message. Images. More text, more text. Lorem impsum dolor sit amet.

    **/

        //IMAP Functions begin here (Before NewsUpdate function)
            //Accessing e-mail inbox
            $mailbox = '{server.com/imap/novalidate-cert}INBOX';
            $mbox = imap_open($mailbox, 'user', 'password'); // log in to mail server



        $no = 2; //number of the e-mail message. Defined as first e-mail in the inbox (not a UID)
            $open_email_msg = imap_body($mbox, $no);


                    //This defines the function "NewsUpdate"
                    function NewsUpdate ($open_email_msg) 
                    {

                            //Login to MySQL Datebase
                            $hostname = "localhost";
                            $db_user = "user";
                            $db_password = "password";
                            $database = "database";
                            $db_table = "newsentries";

                            $db = mysql_connect($hostname, $db_user, $db_password);
                            mysql_select_db($database,$db);

                            //These functions searches the open e-mail for the the prefix defining strings.
                                //Need a function to search after the space after the strings because the subject, categories, snippet, tags and message are constant-changing.
                                $subject = preg_match('/Title: (.*)/', $open_email_msg, $matches) ? $matches[1] : '';
                                    $subject = str_replace("Title: ", "" ,$subject);

                                $categories = preg_match('/Categories: (.*)/', $open_email_msg, $matches) ? $matches[1] : '';       //Searches the open e-mail for the string "Categories"
                                    $categories = str_replace("Categories: ", "" ,$categories);

                                $snippet = preg_match('/Snippet: (.*)/', $open_email_msg, $matches) ? $matches[1] : '';                 //Searches the open e-mail for the string "Snippet"
                                    $snippet = str_replace("Snippet: ", "" ,$snippet);

    ##########################################################                              
                                $content = preg_match('/Message: (.*)/', $open_email_msg, $matches) ? $matches[1] : '';             //Searches the open-email for the string "Message"
                                    $content = str_replace("Message: ", "" ,$content);

    #########################################################

                                //This defines the article's tags
                                $tags = str_replace(' ',',',$subject);  //DDIE

                                $when = strtotime("now");       //date article was posted


                            # THIS CODE WILL TELL MYSQL TO INSERT THE DATA FROM THE EMAIL INTO YOUR MYSQL TABLE
                        $sql = "INSERT INTO $db_table(`caption`,`snippet`,`content`,`when`,`tags`,`categories`,`DATE`) values ('$subject','$snippet','$content','$when','$tags','$categories','$when')";
                            if($result = mysql_query($sql ,$db)) {
                            } else {
                            echo "ERROR: ".mysql_error();
                            }
                            //echo "<h1>News Article added!</h1>"; //uncomment for testing purposes

                    }   //end definung the function NewsUpdate


                //This script that searches the e-mail for the string "News Update", and if it is found it will start the function NewsUpdate
                //if(strpos($open_email_msg,"News Update")) 
                    NewsUpdate ($open_email_msg);
    /*      else {
        echo "<b>News Update</b> not found in e-mail message!";
                    }
        */          
                //Deletes the email
    //          imap_delete($mbox, $no);
    //      imap_expunge($mbox);

            //closes the mailbox
        imap_close($mbox); 


    ?>

0 个答案:

没有答案