繁体   English   中英

CakePHP-使用IMAP访问邮件

[英]CakePHP- Accessing mail using IMAP

重新访问上一个问题中指定的问题 ,我尝试并尝试了不同的帐户(尝试使用gmail和Outlook),但问题仍然存在。 如果尝试访问我的Google帐户,则会收到以下错误消息

Error: Unable to get imap_thread after 4 retries. 'Can't open mailbox {imap.gmail.com:993/ssl/imap/tls/novalidate-cert}INBOX: invalid remote specification'

如果我尝试使用我的Outlook帐户访问电子邮件,则错误是相同的:

Error: Unable to get imap_thread after 4 retries. 'Can't open mailbox {outlook.office365.com:993/ssl/imap/tls/novalidate-cert}INBOX: invalid remote specification'

我的设置如下:

public $emailTicket = array(
        'datasource' => 'ImapSource',
        'server' => 'outlook.office365.com',
        'connect' => 'imap/tls/novalidate-cert',
        'username' => 'my email here',
        'password' => 'my password here',
        'port' => '993', //incoming port 
        'ssl' => true,
        'encoding' => 'UTF-8',
        'error_handler' => 'php',
        'auto_mark_as' => array(
        'Seen',
        // 'Answered',
        // 'Flagged',
        // 'Deleted',
        // 'Draft',
        ),
    );

我正在本地计算机上工作,有人知道这是否是问题吗? 有没有人尝试过为他/她工作? 我愿意接受所有意见!

我似乎在这里找不到问题,我已经在这里待了2天了,因此,如果有人可以提供帮助,我将不胜感激! 这也是Nicolas Ramy所使用的插件的链接。

您可以使用以下实现的代码来满足您的要求:

public function generate_email_response_pdf()
{
$this->layout = false;
$this->autoRender = false;

$username = EMP_SMTP_MAIL_FROM;
    $password = EMP_SMTP_MAIL_PASSWORD;

$imap = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', $username, $password);
$emails = imap_search($imap, 'ALL');
if(!empty($emails))
{
    //put the newest emails on top
        rsort($emails);
    foreach($emails as $email_number)
    {
    $flag = 0;
    $mail_data = array();
    $file_name = array();
    $output = array();
    $savefilename = null;
    $filename = null;

    $overview = imap_fetch_overview($imap, $email_number, 0);
    //initialize the subject index with -000, considering not receving this will not be received in
    //subject line of email
    $output['subject'] = '-000x';
    if(isset($overview[0] -> subject))
    {
        $output['subject'] = $overview[0] -> subject;
        }

    $structure = imap_fetchstructure($imap, $email_number);
    if(property_exists($structure, 'parts'))
    {
        $flag = 1;
        $flattened_parts = $this->flatten_parts($structure->parts);
        foreach($flattened_parts as $part_number => $part)
        {
        switch($part->type)
        {
            case 0:
            //the HTML or plain text part of the email
            if((isset($part->subtype)=='HTML')&&(isset($part->disposition)=='ATTACHMENT'))
            {
                $part_number = 1.2;
            }
            else if(isset($part->subtype)=='HTML')
            {
                $part_number = $part_number;
            }
            else
            {
                $part_number = $part_number;
            }               
            $message = $this->get_part($imap, $email_number, $part_number, $part->encoding);
            //now do something with the message, e.g. render it
            break;

            case 1:
            // multi-part headers, can ignore
            break;

            case 2:
            // attached message headers, can ignore
            break;

            case 3: // application
            case 4: // audio
            case 5: // image
            case 6: // video
            case 7: // other
            break;
        }
        if(isset($part->disposition))
        {
            $filename = $this->get_filename_from_part($part);
            if($filename)
            {
            // it's an attachment
            $attachment = $this->get_part($imap, $email_number, $part_number, $part->encoding);

            $file_info = pathinfo($filename);
            $savefilename = RESPONSE_ATTACHMENT_PREFIX.$file_info['filename'].'_'.$this->_getRandId(4).'.'.$file_info['extension'];
            $file_name[] = $savefilename;

            $attachment_file_name =  $this->save_attachment($attachment, $savefilename, $directory_path);
            //imap_fetchbody($imap, $email_number, 2); //This marks message as read
            }
            else
            {
            // don't know what it is
            }
        }
        }
    }
    else
    {
        $encoding = $structure->encoding;
        $message = imap_fetchbody($imap, $email_number, 1.2);
        //echo $message; die;
        if($message == "")
        {
        $message = imap_body($imap, $email_number);
        if($encoding == 3)
        {
            $message = base64_decode($message);
        }
        else if($encoding == 4)
        {
            $message = quoted_printable_decode($message);
        }
        }   
    }    
    $header = imap_headerinfo($imap, $email_number);

    $from_email = $header->from[0]->mailbox."@".$header->from[0]->host;
    $to_email = $header->to[0]->mailbox."@".$header->to[0]->host;
    $reply_to_email = $header->reply_to[0]->mailbox."@".$header->reply_to[0]->host;

    $cc_email = array();
    if(isset($header->cc))
    {
        foreach($header->cc as $ccmail)
        {
        $cc_email[] = $ccmail->mailbox.'@'.$ccmail->host;
        }
        $cc_email = implode(", ", $cc_email);
    }
    $output['to'] = $to_email;
    $output['from'] = $from_email;
    $output['reply_to'] = $reply_to_email;
    $output['cc'] = $cc_email;

    $formatted_date = date('D, d M Y h:i A', strtotime($overview[0] -> date));
    $output['date'] = $formatted_date;
    $output['message'] = $message;
    $output['flag'] = $flag;
    $mail_data['Attachment'] = $file_name;
    $mail_data['Data'] = $output;
    $this->set('response_data', $mail_data);
    $mail_content = null;
    if(!empty($mail_data))
    {
        $this->viewPath = 'Elements/default';
        $mail_content = $this->render('pdf_content');
    }
    $header = null;
    $footer = null;
    $html = preg_replace(array('/[^\r\n\t\x20-\x7E\xA0-\xFF]*/'), '', $mail_content);
    $pdfFile = $this->_generateWkPdf($html, $directory_path, $new_file_name, $header, $footer);

    $image_type = EXT_JPG; 
    $response_files_array = $this->_generateImagesFromPdf($directory_path.$pdfFile, $directory_path, $new_file_name, $image_type);
    }
}
imap_close($imap);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM