Email by Yiimailer, Call Outlook, IMAP

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

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.

  1. 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();
        }
  1. 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
  1. 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);
0 0
6 followers
Viewed: 19 426 times
Version: Unknown (update)
Category: How-tos
Written by: Scott_Huang
Last updated by: CeBe
Created on: Feb 28, 2014
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles