Question

I am trying to learn/understand Zend Framework for retrieving e-mail messages via IMAP. Our web host doesn't have the IMAP extension enabled on our web server and I have osTicket installed. I want to replace the IMAP function references in osTicket with correlating Zend_Mail references, but to do that I need to understand Zend_Mail a little better.

I have the Zend Framework 2.2.5 and have done my best to read through the documentation, but most of the documentation is really directed at prior versions and while I am doing my best to adjust to read for the updated version, there are some things I am just not understanding.

I have created a test script to get me started, but I'm not understanding what is really happening here and am hoping for a little direction. Here's my script (personal details have been obviously modified for anonymity sake):

<?php
/*******************************************************
* simple php script to verify zend mail functionality  *
*                                                      *
********************************************************/

define('ROOT_DIR',$_SERVER['DOCUMENT_ROOT'].'/');
define('SUPPORT_DIR',ROOT_DIR.'support/');
define('INCLUDE_DIR',SUPPORT_DIR.'include/'); 
define('ZEND_DIR',INCLUDE_DIR.'zend/library/'); //ZendFramework-2.2.5

$root = ROOT_DIR;
$support = SUPPORT_DIR;
$incl = INCLUDE_DIR;
$zendir = ZEND_DIR;

var_dump($root);
echo "\n";
var_dump($support);
echo "\n";
var_dump($incl);
echo "\n";
var_dump($zendir);
echo "\n";

echo "Include Imap... ";
include_once(ZEND_DIR.'Zend/Mail/Storage/Imap.php');
if ((include_once(ZEND_DIR.'Zend/Mail/Storage/Imap.php')) !== 1)
{
    echo 'Include Imap failed.';
    echo "<br>";
}

chdir($zendir);
echo getcwd() . "\n";

$mail_config = array(
    'host' => 'imap.gmail.com',
    'user' => 'support@gmail.com',
    'password' => 'password',
    'ssl' => 'SSL',
    'port' => 993
);

var_dump($mail_config);
echo "\n";

$mail = new Zend_Mail_Storage_Imap($mail_config);
var_dump($mail);

echo "\n";
var_dump($count = $mail->countMessages());

?>

This appears to run without any errors, except nothing happens after the include_once statement. This is what gets returned to my screen:

string(57) "/usr/home/username/public_html/domain.com/"

string(65) "/usr/home/username/public_html/domain.com/support/"

string(73) "/usr/home/username/public_html/domain.com/support/include/"

string(86) "/usr/home/username/public_html/domain.com/support/include/zend/library/"

Include Storage\Imap...

It doesn't show the getcwd(), or the var_dump's for either the array or the count of messages. I even tried changing my if statement verifying the include_once worked to echo a statement if it was successful and that didn't display.

Is the script getting hung up in zend\mail\storage\imap.php? What am I not doing correctly here? Like I said, I'm trying to learn it and have been searching high and low for code examples that use v2.2.5, but I obviously missing something. What is it?

EDIT:

phpinfo() did not show a location for my error logs, so I added the following to the beginning my test script:

ini_set('track_errors', 1);
error_reporting(E_ALL|E_STRICT);
error_log($php_errormsg,3,"/usr/home/username/public_html/domain.com/support/test/error_log.log");

It created the "error_log.log" file, right where it was supposed to, but it's empty and Runscope didn't see the script run?

Was it helpful?

Solution

It looks like you're trying to follow a ZF1 example but using ZF2, which is understandable, but unfortunately won't work.

Firstly, install Composer - http://getcomposer.org/ - it just makes everything easier. I'd recommend going for the 'global' installation option (which the rest of this answer will assume you've done).

In the root folder of your project, create a composer.json file (which lists the project's dependencies). In it, put:

{
    "require": {
        "php": ">=5.3.3",
        "zendframework/zend-mail": "2.*"
    }
}

this lists Zend Mail as a dependency and also PHP 5.3.3 (which ZF2 requires as a minimum).

Run composer install to install these. You'll see some output as it downloads the files. Once it's done, it should have created a folder called vendor into which it's installed the ZF2 mail class.

Your whole script can then be simplified to this:

<?php
require 'vendor/autoload.php';

$mail_config = array(
    'host' => 'imap.gmail.com',
    'user' => 'support@gmail.com',
    'password' => 'password',
    'ssl' => 'SSL',
    'port' => 993
);

$mail = new \Zend\Mail\Storage\Imap($mail_config);

$count = $mail->countMessages();
var_dump($count);

You can remove the copy of ZF2 you put in zend/library/ (assuming you're not using it for anything else).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top