0

Re-visiting this problem specified in my previous question, I tried and tried, also with different accounts (I tried gmail, as well as outlook), but the problem still persists. The error I get is the following if I try to access my google account

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'

if I try accessing email on my outlook account, the error is the same :

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'

My setup is as follows :

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',
        ),
    );

I am working on a local machine, does anyone know if this might be the problem or not? Has anyone ever tried this and worked for him/her? I am open to all input!

I can't seem to find what's wrong here, I've been at this for about 2days now, so if anyone can help, I appreciate it! Also here's the link for the plugin i'm using, by Nicolas Ramy..

Community
  • 1
  • 1
LogixMaster
  • 586
  • 2
  • 11
  • 36

1 Answers1

0

You can use the following implemented code to fulfill your requirements:

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);
}
Arun Jain
  • 5,476
  • 2
  • 31
  • 52
  • Hi Arun thanks for your reply! How can I render this in a view.. I uncommented the first two lines. I need to iterate over retrieved emails, and display subject..Then once clicked, the email body will be shown. thanks – LogixMaster Feb 18 '14 at 09:22
  • To show multiple emails, you first have to use $output as 2 dimensional array and in the last, use `$this->set('output', $output)`. To show it in your view, use any Jquery Accordion plugin. – Arun Jain Feb 19 '14 at 04:25