CI框架利用PHPEmail发送邮件

首先去下载PHPEmail类,这里就不再做介绍了

下载完了之后,然后进行调用

在 email_helper.php页面里面代码如下:

include("class.phpmailer.php");
include("class.smtp.php");
//发送邮件
 function sendemail($to, $body, $subject){
        
        $mail             = new PHPMailer(true);
        $mail->IsSMTP();
        $mail->SMTPAuth   = true;  
        $mail->Port       = 25;                  
        //$mail->Host       = "smtp.qq.com";   //腾讯的 
        $mail->Host       = "smtp.163.com";
        $mail->Username   = "[email protected]";  
        $mail->Password   = "zhangmenlove";            
        $mail->From       = "[email protected]";    
        $mail->FromName   = "测试邮件发送";              
        $mail->Subject    = "$subject";        
        $mail->AltBody    = "This is the body when user views in plain text format"; 
        $mail->WordWrap   = 50; 
        $mail->MsgHTML($body);
        $mail->AddReplyTo("[email protected]","Webmaster");
        $mail->AddAddress( $to );
        
        
        $mail->IsHTML(true); 
        if(!$mail->Send()) {
            return false;
        } else {
            return true;
        }
    }

然后在控制器里面:

<?php
class Index extends CI_Controller {
    function __construct() {
    
        header ( "Cache-Control: no-cache, must-revalidate" );
        header ( "Pragma: no-cache" );
        
        parent::__construct();
        $this->load->helper ( array ('form', 'url', 'cookie' ) );
        
    }

    function email(){
           $name    = urldecode($_REQUEST['name']);    
           $phone   = urldecode($_REQUEST['phone']);
           $email   = urldecode($_REQUEST['email']);
           $weburl  = urldecode($_REQUEST['weburl']);
           $company = urldecode($_REQUEST['company']);
           $content = urldecode($_REQUEST['content']);
           //发送邮件给我
           $subject = "您在这里留言了";
           $body_text='<style type="text/css">
                        .tables{ width:600px; margin:40px 50px;}
                        .tables table{ border-collapse:collapse; font-family:"微软雅黑"; font-size:14px}
                        .tables table tr{ height:40px}
                       </style>';
           $body_text.='
           <div class="tables">
            <table  border="1" class="tables" bordercolor="#ccc">
              <tr>
                <td width="20%" align="right">姓名:</td>
                <td style="padding-left:10px">'.$name.'</td>
              </tr>
              <tr>
                <td align="right">电话:</td>
                <td style="padding-left:10px">'.$phone.'</td>
              </tr>
              <tr>
                <td align="right">邮箱:</td>
                <td style="padding-left:10px">'.$email.'</td>
              </tr>
              <tr>
                <td align="right">网址:</td>
                <td style="padding-left:10px">'.$weburl.'</td>
              </tr>
              <tr>
                <td align="right">公司名称:</td>
                <td style="padding-left:10px">'.$company.'</td>
              </tr>
              <tr>
                <td align="right">留言内容:</td>
                <td style="padding-left:10px">'.$content.'</td>
              </tr>
            </table>
            </div>
           ';
           sendemail($email,$body_text,$subject);
    }
}

这些代码都是可以实现发送邮件的



你可能感兴趣的:(CI,邮件发送)