Zend_Mail 利用 GMail 發信
基本上與 PHPMailer 利用 GMail 發信 一模一樣的操作,只是換個工具,從 PHPMailer 換成 Zend Framework 提供的 Zend_Mail 物件實做。操作一樣是不需要去設定 php.ini 內的 sendmail 以及 port,完全跟現行的系統平台切割。
環境準備
假設您已經具備 Zend Framework 專案環境
操作流程
1. 引用 Zend_Mail_Transport_Smtp 物件
2. 建立 Mail.php 放置寄信的主程式
3. 撰寫寄信主程式 init()
如何使用上述建立好的寄信程式?
谷歌 SMTP 設定
如上程式碼內所示,主要有以下參數
Referecne
[1.] Zend 官方網站::SMTP Authentication
[2.] 寶尼::PHPMailer 利用 GMail 發信
環境準備
假設您已經具備 Zend Framework 專案環境
操作流程
1. 引用 Zend_Mail_Transport_Smtp 物件
require_once 'Zend/Mail/Transport/Smtp.php';我們將用到 Zend_Mail_Transport_Smtp 以及 Zend_Mail 兩個物件。
2. 建立 Mail.php 放置寄信的主程式
class Ncku_SystemMail
{
// 將用來儲存 Zend_Mail 實例
protected $mail;
protected $authDetail = array(
'ssl' => 'ssl',
'port' => 465,
'auth' => 'login',
'username' => 'yourusername@gmail.com',
'password' => 'yourpassword'
);
public function __construct() {
// 設定 SMTP Authentication
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $this->authDetail);
Zend_Mail::setDefaultTransport($transport);
// 建立 Zend_Mail
$this->mail = new Zend_Mail();
$this->init();
}
}
3. 撰寫寄信主程式 init()
public function init(){
$this->mail->setBodyText('This is the text of the mail.');
$this->mail->setFrom('yourusername@gmail.com', 'Some Sender');
$this->mail->addTo('someone@gmail.com', 'Some Recipient');
$this->mail->setSubject('TestSubject');
$this->mail->send();
}
如何使用上述建立好的寄信程式?
new Mail();建立物件實體即可,它就會盡信到您所設定的信箱囉。
谷歌 SMTP 設定
如上程式碼內所示,主要有以下參數
SMTP: smtp.gmail.com Port: 465 SSL: ssl
Referecne
[1.] Zend 官方網站::SMTP Authentication
[2.] 寶尼::PHPMailer 利用 GMail 發信
Comments
Post a Comment