目录

CodeIgniter - 发送电子邮件( Sending Email)

在CodeIgniter中发送电子邮件要容易得多。 您还可以在CodeIgniter中配置有关电子邮件的首选项。 CodeIgniter提供以下发送电子邮件的功能 -

  • 多个协议 - 邮件,Sendmail和SMTP
  • SMTP的TLS和SSL加密
  • 多个收件人
  • CC and BCCs
  • HTML或纯文本电子邮件
  • Attachments
  • Word wrapping
  • Priorities
  • BCC批处理模式,可以将大型电子邮件列表分成小的BCC批次。
  • 电子邮件调试工具

电子邮件类具有以下功能,以简化发送电子邮件的工作。

SN 句法 参数 返回 退货类型
1 from( $from [, $name = '' [, $return_path = NULL ]])

$fromstring ) - “From”电子邮件地址

$namestring ) - “From”显示名称

$return_pathstring ) - 用于将未传递的电子邮件重定向到的可选电子邮件地址

CI_Email instance (method chaining)CI_Email
2 reply_to( $replyto [, $name = '' ])

$replytostring ) - 回复的电子邮件地址

$namestring ) - 显示回复电子邮件地址的名称

CI_Email instance (method chaining)CI_Email
2to( $to )

$tomixed ) - 以逗号分隔的字符串或一组电子邮件地址

CI_Email instance (method chaining)CI_Email
3cc( $cc )

$ccmixed ) - 以逗号分隔的字符串或一组电子邮件地址

CI_Email instance (method chaining)CI_Email
4 密送( $bcc [, $limit = '' ])

$bccmixed ) - 以逗号分隔的字符串或一组电子邮件地址

$limitint ) - 每批发送的最大电子邮件数

CI_Email instance (method chaining)CI_Email
5subject( $subject )

$subjectstring ) - 电子邮件主题行

CI_Email instance (method chaining)CI_Email
6message( $body )

$bodystring ) - 电子邮件正文

CI_Email instance (method chaining)CI_Email
7set_alt_message( $str )

$strstring ) - 备用电子邮件正文

CI_Email instance (method chaining)CI_Email
8set_header( $header, $value )

$headerstring ) - 标题名称

$valuestring ) - 标头值

CI_Email instance (method chaining)CI_Email
9 清除([ $clear_attachments = FALSE ])

$clear_attachmentsbool ) - 是否清除附件

CI_Email instance (method chaining)CI_Email
10 发送([ $auto_clear = TRUE ])

$auto_clearbool ) - 是否自动清除消息数据

CI_Email instance (method chaining)CI_Email
11 attach($ filename [,$ disposition =''[,$ newname = NULL [,$ mime ='']]])

$filenamestring ) - 文件名

$dispositionstring ) - 附件的“处置”。 无论此处使用的MIME规范如何,大多数电子邮件客户端都会自行决定。 iana

$newnamestring ) - 要在电子邮件中使用的自定义文件名

$mimestring ) - 要使用的MIME类型(对缓冲数据有用)

CI_Email instance (method chaining)CI_Email
12attachment_cid( $filename )

$filenamestring ) - 现有附件文件名

附件Content-ID如果找不到,则为FALSE string

发送电子邮件

要使用CodeIgniter发送电子邮件,首先必须使用以下方法加载电子邮件库 -

$this->load->library('email');

加载库后,只需执行以下功能即可设置发送电子邮件所需的元素。 from()函数用于设置 - 从发送电子邮件to()函数 - 向谁发送电子邮件。 subject()message()函数用于设置电子邮件的主题和消息。

$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

之后,执行send()函数,如下所示发送电子邮件。

$this->email->send();

例子 (Example)

创建一个控制器文件Email_controller.php并将其保存在application/controller/Email_controller.php

<?php 
   class Email_controller extends CI_Controller { 
      function __construct() { 
         parent::__construct(); 
         $this->load->library('session'); 
         $this->load->helper('form'); 
      } 
      public function index() { 
         $this->load->helper('form'); 
         $this->load->view('email_form'); 
      } 
      public function send_mail() { 
         $from_email = "your@example.com"; 
         $to_email = $this->input->post('email'); 
         //Load email library 
         $this->load->library('email'); 
         $this->email->from($from_email, 'Your Name'); 
         $this->email->to($to_email);
         $this->email->subject('Email Test'); 
         $this->email->message('Testing the email class.'); 
         //Send mail 
         if($this->email->send()) 
         $this->session->set_flashdata("email_sent","Email sent successfully."); 
         else 
         $this->session->set_flashdata("email_sent","Error in sending Email."); 
         $this->load->view('email_form'); 
      } 
   } 
?>

创建一个名为email_form.php的视图文件,并将其保存在application/views/email_form.php

<!DOCTYPE html> 
<html lang = "en"> 
   <head> 
      <meta charset = "utf-8"> 
      <title>CodeIgniter Email Example</title> 
   </head>
   <body> 
      <?php 
         echo $this->session->flashdata('email_sent'); 
         echo form_open('/Email_controller/send_mail'); 
      ?> 
      <input type = "email" name = "email" required /> 
      <input type = "submit" value = "SEND MAIL"> 
      <?php 
         echo form_close(); 
      ?> 
   </body>
</html>

application/config/routes.php中的routes.php文件中进行更改,并在文件末尾添加以下行。

$route['email'] = 'Email_Controller';

通过访问以下链接执行上面的示例。 将yoursite.com替换为您网站的网址。

http://yoursite.com/index.php/email
↑回到顶部↑
WIKI教程 @2018