CI(CodeIgniter) 实现网站在线自动发送邮件

在平时,我们在网络上注册很多服务的时候,都会收得到相应网站自动发过来的如确认邮件等实时邮件。极大的方便网站实时,准确抓住有效用户。
看起来感觉门槛很高,好像很大型的网站的技术团队才能实现到。其实不然,就算我们是小团队,甚至一个人,也可以轻松的实现这样的功能。
在这里,我介绍的是使用 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('[email protected]', 'sender name'); // show in the reciever email box
$this->email->to($_POST['email']);
//$this->email->cc('[email protected]');
//$this->email->bcc('[email protected]');
$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 可以找到。
这样就可以实现在线自动发邮件给用户了。赶快去自定义自己的信件内容呗。
Tags: 

延伸阅读

最新评论

发表评论