zoukankan      html  css  js  c++  java
  • 在CI中集成phpmailer,方便使用SMTP发送邮件

    直接使用phpmailer的话,有时候不是很方便,特别你的很多功能都是基于CI完成的时候,要相互依赖就不方便了,所以在想,那是否可以将phpmailer集成到CI中呢,像使用email类这样使用他,功夫不负有心人,在网上居然有人分享了很多内容,但是之前的CI是支持插件功能的,所以很多文章都是说的基于插件的方式,现在CI有了新的调整,基于类的方式。最后找到一篇文章,可以帮助我们解决这个问题,将phpmailer集成到CI中,成为类,大家可以去到这个url查看详细的介绍:http://blog.qoding.us/2011/09/codeigniter-using-phpmailer-to-send-email-via-gmail/

    最近要處理一個電子報系統,再用 CI 那跛腳 Email Class 大概會被客訴到瘋掉。所以還是認命改用老牌的 PHPMailer Library。稍微試一下,發現在 CI 裡使用 PHPMailer 相當無痛,先到官網下載一份 PHPMailer (本文完成時的最新版本是 5.2.0),解壓縮後把整個資料夾丟到 CI\application\libraries\PHPMailer_5.2.2。接著在 libraries 下建立新檔案,就叫 mailer.php 好了。

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
     
    class Mailer {
     
        var $mail;
     
        public function __construct()
        {
            require_once('PHPMailer_5.2.2/class.phpmailer.php');
     
            // the true param means it will throw exceptions on errors, which we need to catch
            $this->mail = new PHPMailer(true);
     
            $this->mail->IsSMTP(); // telling the class to use SMTP
     
            $this->mail->CharSet = "utf-8";                  // 一定要設定 CharSet 才能正確處理中文
          //  $this->mail->SMTPDebug  = 0;                     // enables SMTP debug information
            $this->mail->SMTPAuth   = true;                  // enable SMTP authentication
          //  $this->mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
            $this->mail->Host       = "smtp.163.com";      // sets 163 as the SMTP server
            //$this->mail->Port       = 465;                   // set the SMTP port for the 163 server
            $this->mail->Username   = "xxxxxx@163.com";// 163 username
            $this->mail->Password   = "xxxxxxxxx";       // 163 password
           /// $this->mail->AddReplyTo('@163.com', 'YOUR_NAME');	 //回复地址(可填可不填)
            //$this->mail->SetFrom('YOUR_GAMIL@163.com', 'YOUR_NAME'); 
        }
     
        public function sendmail($to, $to_name, $subject, $body){
            try{
                $this->mail->From = 'xxxx@163.com';
                $this->mail->FromName = 'xxxxxx';
                $this->mail->AddAddress($to, $to_name);
                
                $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
                $this->mail->IsHTML(true);                                  // 使用html格式
    
                $this->mail->Subject = $subject;
                $this->mail->Body    = $body;
    
     
                $this->mail->Send();
                    echo "Message Sent OK</p>
    ";
     
            } catch (phpmailerException $e) {
                echo $e->errorMessage(); //Pretty error messages from PHPMailer
            } catch (Exception $e) {
                echo $e->getMessage(); //Boring error messages from anything else!
            }
        }
    }
     
    /* End of file mailer.php */
    

    接著在 Controller 裡呼叫這支 library 就可以了,範例如

    <?php
    
    		class Message extends CI_Controller{
    			public function __construct(){
    			        parent::__construct();
    			 }
    
    
    			public function send(){
    				        $mail_body = "这是主题的内容";
    				        $this->load->library('mailer');
    				        $this->mailer->sendmail(
    				            'it6780@qq.com',
    				            'charles',
    				            '這是測試信 '.date('Y-m-d H:i:s'),
    				            $mail_body
    				        );
    		
    			}	
    		}	
    

      

    下。

      

  • 相关阅读:
    Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class
    移动商城第三篇【搭建Mybatis和Spring环境、编写Dao、Service在Core模块】
    移动商城第二篇【页面框架解析】
    移动商城第一篇【搭建项目环境】
    idea下使用Maven找不到类
    Oracle与Mysql区别简述
    Shiro第六篇【验证码、记住我】
    Shiro第五篇【授权过滤、注解、JSP标签方式、与ehcache整合】
    Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
    Shiro第三篇【授权、自定义reaml授权】
  • 原文地址:https://www.cnblogs.com/wicub/p/3178877.html
Copyright © 2011-2022 走看看