用于从电子邮件帐户中读取已发送电子邮件的PHP代码

时间:2018-02-13 07:43:36

标签: php email

作为我项目的一部分,我需要阅读已发送的电子邮件和从电子邮件帐户收到邮件(收件箱) 我已设法使用以下代码实现阅读收到的邮件(收件箱)。它正在努力工作

set_time_limit(4000); 

// Connect to gmail

$imapPath = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'test@gmail.om';
$password = '*********';

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


// search and get unseen emails, function will return email ids
//$emails = imap_search($inbox,'UNSEEN');
 $emails = imap_search($inbox,'SUBJECT "royal enfield"');
$output = '';
 if (is_array($emails) || is_object($emails))
{
foreach($emails as $mail) {
   $overview = imap_fetch_overview($inbox,$mail,0); 
    $headerInfo = imap_headerinfo($inbox,$mail);

    $output .= '<b>Subject:- </b>'.$headerInfo->subject.'<br/><br/>';
    $output .= '<b>Toaddress:- </b>'.$headerInfo->toaddress.'<br/><br/>';
    $output .= '<b>Date:- </b>'.$headerInfo->date.'<br/><br/>';
    $output .= '<b>fromaddress:- </b>'.$headerInfo->fromaddress.'<br/><br/>';
    $output .= '<b>reply_toaddress:- </b>'.$headerInfo->reply_toaddress.'<br/><br/>';
    $output .= '<b>Status:- </b>'.$overview[0]->seen ? 'read' : 'unread'.'<br/><br/>';


        $emailStructure = imap_fetchstructure($inbox,$mail);

    if(!isset($emailStructure->parts)) {
         $output .= imap_body($inbox, $mail, FT_PEEK); 
    } else {
        //    
    }
   echo $output;

   $output = '';
}
}
}
// colse the connection
imap_expunge($inbox);
imap_close($inbox); 

我希望有人能帮我看已发邮件 提前谢谢。

2 个答案:

答案 0 :(得分:1)

您需要在代码中使用另一个$ imapPath(str 5)

对于Gmail, usualy '{imap.gmail.com:993/ssl}[Gmail]/Sent Mail'

所以它将是

$imapPath = '{imap.gmail.com:993/ssl}[Gmail]/Sent Mail';

如果没有帮助,请尝试使用以下代码获取所有文件夹

$imapPath = '{imap.gmail.com:993/ssl}';
$mail_con = imap_open($imapPath, $username, $password);
$mailboxes = imap_list($mail_con, $imapPath, '*');
print_r($mailboxes);

你会得到像这样的somithing

Array
(
  [0] => {imap.gmail.com:993/ssl}INBOX
  [1] => {imap.gmail.com:993/ssl}Sended
  [2] => {imap.gmail.com:993/ssl}[Gmail]/All Mail
  [3] => {imap.gmail.com:993/ssl}[Gmail]/Drafts
  [4] => {imap.gmail.com:993/ssl}[Gmail]/Important
  [5] => {imap.gmail.com:993/ssl}[Gmail]/Sent Mail
  [6] => {imap.gmail.com:993/ssl}[Gmail]/Spam
  [7] => {imap.gmail.com:993/ssl}[Gmail]/Starred
  [8] => {imap.gmail.com:993/ssl}[Gmail]/Trash
  ....

你可以用作$ imapPath

的任何一个

如果您的Gmail不是英文,则可以对文件夹名进行编码。 这是每个文件夹通过3封电子邮件获取的代码。它会帮助你找到所需的

<?php

set_time_limit(4000);

// Connect to gmail


$username = 'test@gmail.om';
$password = '*********';

$imapPath = '{imap.gmail.com:993/ssl}';
$mail_con = imap_open($imapPath, $username, $password);
$mailboxes = imap_list($mail_con, $imapPath, '*');
//print_r($mailboxes);

foreach ($mailboxes as $imapPath) {
   echo "\n\n\n\n\n============. $imapPath";


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


// search and get unseen emails, function will return email ids
//$emails = imap_search($inbox,'UNSEEN');
    $emails = imap_search($inbox, '');
    $output = '';
    $count = 0;
    if (is_array($emails) || is_object($emails)) {
        foreach ($emails as $mail) {
            $overview = imap_fetch_overview($inbox, $mail, 0);
            $headerInfo = imap_headerinfo($inbox, $mail);
            $output .= "\n\n\n";
            $output .= '<b>Subject:- </b>' . $headerInfo->subject . '<br/><br/>';
            $output .= '<b>Toaddress:- </b>' . $headerInfo->toaddress . '<br/><br/>';
            $output .= "\n" . '<b>Date:- </b>' . $headerInfo->date . '<br/><br/>';
            $output .= "\n" . '<b>fromaddress:- </b>' . $headerInfo->fromaddress . '<br/><br/>';
            $output .= '<b>reply_toaddress:- </b>' . $headerInfo->reply_toaddress . '<br/><br/>';
            $output .= '<b>Status:- </b>' . $overview[0]->seen ? 'read' : 'unread' . '<br/><br/>';


            $emailStructure = imap_fetchstructure($inbox, $mail);

            if (!isset($emailStructure->parts)) {
//            $output .= imap_body($inbox, $mail, FT_PEEK);
            } else {
                //
            }
            echo $output;

            $output = '';

            if ($count++ > 2) break;
        }
    }
}
// colse the connection
imap_expunge($inbox);
imap_close($inbox);

答案 1 :(得分:0)

您可以使用以下代码阅读已发送的电子邮件:

$mailboxes = array(
array(
    'label'     => 'domain.com',
    'enable'    => true,
    'mailbox'   => '{mail.domain.com:143/notls}INBOX.Sent',
    'username'  => 'mail@domain.com',
    'password'  => 'mypassword'
)
);
$stream = @imap_open($current_mailbox['mailbox'], $current_mailbox['username'], $current_mailbox['password']);