???? 在平時,我們在網絡上注冊很多服務的時候,都會收得到相應網站自動發過來的如確認郵件等實時郵件。極大的方便網站實時,準確抓住有效用戶。
看起來感覺門檻很高,好像很大型的網站的技術團隊才能實現到。其實不然,就算我們是小團隊,甚至一個人,也可以輕松的實現這樣的功能。
在這里,我介紹的是使用 PHP 代碼實現的,這主要得益于 PHP 的 Email 函數。
是我們可實現在本地調用遠程服務器發送郵件,其實也就是本地將內容按預定的規則封裝好傳送到服務器的特定端口,服務器解析、再投送到指定的郵箱。
好的, 我在這里使用的不是純 PHP 自己寫出來的代碼實現,而是在 PHP 的一個框架 CI ( CodeIgniter ) 上實現。
CI 是一個非常輕巧并功能強大的 PHP 框架,其封裝好了許多的 PHP 函數和用法,再通過其簡單的語法去實現你想要的功能,非常適合想快速開放 PHP 網站的新手。
這里,整個代碼如下:
?
public function send_email()?? // 寫在 Controller 里邊。
??? {
??????? $this->load->library('email');
???????
??????? $config['protocol'] = 'smtp';
??????? $config['smtp_host'] = 'smtp.163.com'; // given server
??????? $config['smtp_user'] = 'your email ';
??????? $config['smtp_pass'] = 'email password';
??????? $config['smtp_port'] = '25'; // given port.
??????? $config['smtp_timeout'] = '5';
??????? $config['newline'] = "/r/n";
??????? $config['crlf'] = "/r/n";
??????? $config['charset']='utf-8';? // Encoding type
????????
??????? $this->email->initialize($config);? //initialize the config
???????
???????
??????? $this->email->from('vip@xx.com', 'sender name');? // show in the reciever email box
??????? $this->email->to($_POST['email']);
??????? //$this->email->cc('another@another-example.com');?
??????? //$this->email->bcc('them@their-example.com');
??????? $diy_subject='Auto reply';? // Email title
??????? $diy_msg='Dear '.$_POST['username'].'Test the content whether is right.';? // Email content
????????
??????? $this->email->subject($diy_subject);
??????? $this->email->message($diy_msg);
??????? $this->email->send();?? //Send out the email.
??????? //echo $this->email->print_debugger();
??????? $data['username']=$_POST['username'];
??????? $data['email']=$_POST['email'];
??????? $this->load->view('send_email_ok',$data);
??? }
具體的 Email 類說明在 http://codeigniter.org.cn/user_guide/libraries/email.html 可以找到。
這樣就可以實現在線自動發郵件給用戶了。趕快去自定義自己的信件內容唄。