使用PHP IMAP打开电子邮件

时间:2011-12-29 18:49:53

标签: php mysql imap

w3schools论坛上的用户协助我使用IMAP功能检查私人服务器上的邮件收件箱并使用它做我想做的事情,我创建了自己的一组功能,用于发送电子邮件内容到一个MySQL表。

有人可以帮我找到解决方法,如何打开电子邮件收件箱,检查收件箱中的电子邮件(因为之前的电子邮件会自动删除,因此只会有一封电子邮件。定义打开的电子邮件消息为$ open_email_msg 允许我启动一组命令,将电子邮件发送到MySQL表,然后删除电子邮件并关闭收件箱?

这是该人协助我的代码:

<?php

$now = time(); // current time

$mailbox = '{192.168.150.11:143/imap/novalidate-cert}'; // see http://www.php.net/manual/en/function.imap-open.php 
$mbox = imap_open($mailbox, 'username', 'password'); // log in to mail server

if (!$mbox)
  echo ('Failed opening mailbox<br>' . print_r(imap_errors(), true)); // remove the print_r for production use
else
{
  $box = imap_check($mbox); // get the inbox

  for ($imap_idx = 1; $imap_idx <= $box->Nmsgs; $imap_idx++) // loop through the messages
  {
    $headers = imap_headerinfo($mbox, $imap_idx); // http://www.php.net/manual/en/function.imap-headerinfo.php
    $raw_headers = imap_fetchheader($mbox, $imap_idx); // http://www.php.net/manual/en/function.imap-fetchheader.php
    $selected_headers = '';
    $text_part = '';
    $html_part = '';
    $original_message = imap_body($mbox, $imap_idx); // save the copy of the entire thing, attachments and all

    // build selected headers string
    for ($ii = 0; $ii < count($headers->from); $ii++)
      $selected_headers .= 'From: ' . $headers->from[$ii]->mailbox . '@' . $headers->from[$ii]->host . "\n";
    for ($ii = 0; $ii < count($headers->to); $ii++)
      $selected_headers .= 'To: ' . $headers->to[$ii]->mailbox . '@' . $headers->to[$ii]->host . "\n";
    for ($ii = 0; $ii < count($headers->cc); $ii++)
      $selected_headers .= 'Cc: ' . $headers->cc[$ii]->mailbox . '@' . $headers->cc[$ii]->host . "\n";
    for ($ii = 0; $ii < count($headers->bcc); $ii++)
      $selected_headers .= 'Bcc: ' . $headers->bcc[$ii]->mailbox . '@' . $headers->bcc[$ii]->host . "\n";
    if (!empty($headers->date))
      $selected_headers .= 'Date: ' . $headers->date . "\n";
    if (!empty($headers->subject))
      $selected_headers .= 'Subject: ' . $headers->subject . "\n";



    // see below; getMsg uses global variables
    getMsg($mbox, $imap_idx);

    $text_part = $plainmsg; // text portion of the email
    $html_part = $htmlmsg; // html portion of the email

    // check for text portion first
    $msg_text = trim(strip_tags($plainmsg

2 个答案:

答案 0 :(得分:0)

尝试使用此代码阅读电子邮件。

     $username=$email; // e.g. test@example.com
         $password='password';

         $hostname = '{example.com:995/pop3/ssl/novalidate-cert}'; 
         $username = $username; $password = $password; $imap = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error());

          $message_count = imap_num_msg($imap);
          // print $message_count;


    for ($i = 1; $i <= $message_count; ++$i) 
            {
                $header = imap_header($imap, $i);
                $body = imap_fetchbody($imap, $i,2);
                $prettydate = date("jS F Y", $header->udate);

                if (isset($header->from[0]->personal)) {
                    $personal = $header->from[0]->personal;
                } else {
                    $personal = $header->from[0]->mailbox;
                }

         $subject=$header->Subject;

     $email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
              echo "On $prettydate, $email said \"$body\".\n";
                echo '<br><br>';
            }

        print_r(imap_errors());
        imap_close($imap);
}   

答案 1 :(得分:0)

我相信this link会帮助你,因为我自己使用这个,这对我来说是正常的。

在那里你可以注册和下载代码,使用它很简单。

如果您只想获取标题信息,可以执行的操作是:

$mbox = imap_open("{xyz@abc.com:995/pop3/ssl/novalidate-cert}INBOX", 'abc@xyz.com', 'pass')
                or die("can't connect: " . imap_last_error());


$MC = imap_check($mbox);
  $result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);

  foreach ($result as $overview) {
    echo "#{$overview->msgno} ({$overview->date}) - From: {$overview->from}
    {$overview->subject}\n";
    echo "<br>";
}