Email by YiiMailer, Call Outlook, IMAP

  1. - YiiMailer Sample
  2. - Call Outlook example
  3. - List email by IMAP.

In Yii community we have lots of extention can send emails, such as YiiMailer and Swift and so on, or even IMAPI, thanks for team contribution. However, sometimes, in the intranet enviroment, the program cannot run successfully since SMTP /IMAP port been banded. So you need to seek other ways. The most simple way is call outlook as COM.

Let me introduce send email by YiiMailer/OutLook/IMAPI below, thanks.

- YiiMailer Sample

//set gmail config firstly in protected\config\mail.php firstly
    'Host' => 'smtp.gmail.com',
    'Port' =>465,
    //'Port' => 25,
    //'Port' => 587,
    'SMTPSecure' => 'ssl',
    'SMTPAuth' => true,
    'From' => 'WWSP.NoReply@gmail.com',
    'FromName' => 'WWSP.NOREPLY',
    'Username' => 'WWSP.NoReply@gmail.com',
    'Password' => 'your password',
//

$mail = new YiiMailer();
        //$mail->clearLayout();//if layout is already set in config
        $mail->setTo($model->Platform_Name);
        //$mail->setTo(array('xxx@qq.com'=>'Scott QQ','WWSP_NOREPLY@qq.com'=>'WWSP QQ'));
        //$mail->setCC('xxx@gmail.com');
        $mail->setSubject('Mail subject :' . $model->Brand_Name . '   ' . time());
        $mail->setBody('Time:' . time() . $model->Note);
        //$mail->setAttachment(array('something.pdf'=>'Some file','something_else.pdf'=>'Another file'));
        $mail->IsSMTP();
        if ($mail->send()) {
            //echo time() . ":send email successfully.<BR />";
            Yii::app()->user->setFlash('contact', time() . ' send mail success. Thank you for contacting us. We will respond to you as soon as possible.');
            //$this->refresh();
            return 1;
        } else {
            //echo time() . ":send email failed.<BR />";
            echo $mail->getError();
            Yii::app()->user->setFlash('error', time() . 'Error while sending email: ' . $mail->getError());
            return -1;
            //$this->refresh();
        }

- Call Outlook example

if (!defined('olMailItem'))
                    define('olMailItem', 0);
                $objApp = new COM("Outlook.Application") or die("Cannot initiate outlook in server.");
                $myItem = $objApp->CreateItem(olMailItem);
                $myItem->Recipients->Add($model->Platform_Name);
                $myItem->SentOnBehalfOfName = 'WWSP_SYSTEM@xxx.Com';
                $myItem->Subject = "[NPI_Tool_Auto_Email] " . $model->Brand_Name;
                //$myItem->Body = 'Time:' . time();
                $myItem->HTMLBody = 'Time:' . time() . $model->Note;
                //$myItem->CC = 'xxx@qq.com';
                //$myItem->BCC = 'xxx@gmail.com';
                //$myItem->attachments->Add("C:\\Users\\scott_huang\\Desktop\\firefox-sample.png");
                //$myItem->attachments->Add("C:\\Users\\scott_huang\\Desktop\\IE-sample.png");
                $myItem->Display();
                if ($myItem->Send()
                )
                    Yii::app()->user->setFlash('Success:', 'Send mail success');
                else
                    Yii::app()->user->setFlash('Error:', 'Send mail failed');
                unset($objApp);
                unset($myItem);

The code is quite simple. The key thing is how to enable DCOM in XAPMM enviroment. In PHP.ini

;turn on by Scott_Huang
extension=php_com_dotnet.dll

; turn on by Scott_Huang
com.allow_dcom = true

; turn on by Scott_Huang
com.autoregister_typelib = true

- List email by IMAP.

$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
        $username = 'WWSP.xxx.NoReply@gmail.com';
        $password = 'xxx';

$inbox = imap_open($hostname, $username, $password) or die('Cannot connect to gmail: ' . imap_last_error());
        $headers = imap_headers($inbox) or die('Could not get emails');
        $numEmails = sizeof($headers);
        echo "You have $numEmails in your mailbox";

        for ($i = 1; $i < $numEmails + 1; $i++) {
            $mailHeader = imap_headerinfo($inbox, $i);
            $from = $mailHeader->fromaddress;
            $subject = strip_tags($mailHeader->subject);
            $date = $mailHeader->date;
            //$body = imap_body($inbox, $i);
            echo "Email from $from, subject $subject, date $date<BR />";
            //echo strip_tags($body) . "<BR />" . "<BR />" . "<BR />";
        }

        imap_close($inbox);