Home >Backend Development >PHP Tutorial >Use phpmailer to send emails in codeigniter's helper_PHP tutorial

Use phpmailer to send emails in codeigniter's helper_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:52:291007browse

然后在helper文件里 写入如下函数

[php] 
function send_mail($to,$title,$body) 

    $ci =& get_instance(); 
    require_once('phpmailer/class.phpmailer.php'); 
    require_once('phpmailer/class.smtp.php'); 
     
    $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch  
 
    $mail->IsSMTP(); // telling the class to use SMTP  
 
    try { 
      $mail->Host       = $ci->config->item('mail_smtp'); // SMTP server  
      $mail->SMTPDebug  = false;                     // enables SMTP debug information (for testing)  
      $mail->SMTPAuth   = true;                  // enable SMTP authentication  
      $mail->Port       = $ci->config->item('mail_port');                    // set the SMTP port for the GMAIL server  
       
      $mail->Username   = $ci->config->item('mail_address'); // SMTP account username  
      $mail->Password   = $ci->config->item('mail_passwd');        // SMTP account password  
       
      $mail->AddAddress(www.2cto.com, ''); 
      $mail->SetFrom($ci->config->item('mail_address'), $ci->config->item('mail_name')); 
      
      $mail->Subject = $title; 
      $mail->MsgHTML($body); 
       
      $mail->Send(); 
      return true; 
    } catch (phpmailerException $e) { 
      //echo $e->errorMessage(); //Pretty error messages from PHPMailer  
      return false; 
    } catch (Exception $e) { 
      //echo $e->getMessage(); //Boring error messages from anything else!  
      return false; 
    } 
         

function send_mail($to,$title,$body)
{
 $ci =& get_instance();
 require_once('phpmailer/class.phpmailer.php');
 require_once('phpmailer/class.smtp.php');
 
 $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

 $mail->IsSMTP(); // telling the class to use SMTP

 try {
   $mail->Host       = $ci->config->item('mail_smtp'); // SMTP server
   $mail->SMTPDebug  = false;                     // enables SMTP debug information (for testing)
   $mail->SMTPAuth   = true;                  // enable SMTP authentication
   $mail->Port       = $ci->config->item('mail_port');                    // set the SMTP port for the GMAIL server
  
   $mail->Username   = $ci->config->item('mail_address'); // SMTP account username
   $mail->Password   = $ci->config->item('mail_passwd');        // SMTP account password
  
   $mail->AddAddress(www.2cto.com, '');
   $mail->SetFrom($ci->config->item('mail_address'), $ci->config->item('mail_name'));
 
   $mail->Subject = $title;
   $mail->MsgHTML($body);
  
   $mail->Send();
   return true;
 } catch (phpmailerException $e) {
   //echo $e->errorMessage(); //Pretty error messages from PHPMailer
   return false;
 } catch (Exception $e) {
   //echo $e->getMessage(); //Boring error messages from anything else!
   return false;
 }
  
}
另外在config里写入你的邮箱配置项

[php] 
$config['mail_name']='焦常云'; 
$config['mail_passwd']='password'; 
$config['mail_address']='xxxx@21cn.com'; 
$config['mail_smtp']='smtp.21cn.com'; 
$config['mail_smtp_port']=25; 
$config['mail_name']='焦常云';
$config['mail_passwd']='password';
$config['mail_address']='xxxx@21cn.com';
$config['mail_smtp']='smtp.21cn.com';
$config['mail_smtp_port']=25;

 作者:jiaochangyun
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478124.htmlTechArticle然后在helper文件里 写入如下函数 [php] function send_mail($to,$title,$body) { $ci = get_instance(); require_once(phpmailer/class.phpmailer.php); require_once(phpmailer/c...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn