【问题标题】:how can I pull out the contents from an email and write them in a mysql table?如何从电子邮件中提取内容并将其写入 mysql 表中?
【发布时间】:2011-12-30 18:35:06
【问题描述】:

当用户向我的电子邮件地址发送电子邮件时,我如何才能将他们的电子邮件文本/信息写入 mysql 表?所以基本上把一封新邮件的内容提取出来,写入mysql表中。

我试过了,但什么也没得到:

<?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); 
} 
?>

我们正在使用交换服务器..我是 coop 学生,所以我在这方面不是很先进。

我尝试了这个作为测试,看看它是否有效,登录 gmail 阅读电子邮件。没用。

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

?>

【问题讨论】:

  • 1.你缺少';' a imap_num_msg($imap) 的结尾; 2. 确保你真的连接到你的服务器。如果您返回 $imap 可能是 FALSE,这意味着未连接 3. 确保您也指定了正确的服务器 - gmail.com 看起来关闭

标签: php mysql email


【解决方案1】:

通过 pop3 或 imap 读取您的邮箱并将新电子邮件添加到您的数据库中

看看这个类:http://php.net/manual/en/book.imap.php

【讨论】:

    【解决方案2】:

    您可以每隔一段时间阅读 pop3/imap。
    另一种方法是将您的电子邮件重定向到 php 脚本。我不知道您在服务器上使用的是哪个电子邮件系统,但这里是example with postfix

    pop3/imap 方法更简单,您不需要编辑服务器配置,但第二种方法更快,因为您的脚本将在收到电子邮件时启动。

    【讨论】:

      【解决方案3】:

      为此使用 IMAP 函数。如有必要,请设置另一个电子邮件帐户。下面是示例代码:

      // connect to the mailbox
      $m_mail = imap_open("{mail.YOURHOST.com:993/imap/ssl/novalidate-cert}INBOX", "address@YOURHOST.com", "YOURPASSWORD");
      
      //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 );
      
          //
         DO WHAT YOU NEED TO DO HERE - insert to the database, etc
      
      }
      
      //purge messages (if necessary)
      imap_expunge($m_mail);
      
      //close mailbox 
      imap_close($m_mail);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 2012-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-03
        相关资源
        最近更新 更多