CodeIgniter发送电子邮件


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

  • 多种协议 - Mail,Sendmail和SMTP
  • 用于SMTP的TLS和SSL加密
  • 多个收件人
  • CC和BCC
  • HTML或纯文本电子邮件
  • 附件
  • Word包装
  • 优先级
  • BCC批处理模式,可以将大量电子邮件列表分解为小BCC批次。
  • 电子邮件调试工具

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

序号 语法 参数 返回值 返回值类型
1 from($from[, $name = ''[, $return_path = NULL]])

$from (string) − “From” e-mail address

$name (string) − “From” display name

$return_path (string) − Optional email address to redirect undelivered e-mail to

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

$replyto (string) − E-mail address for replies

$name (string) − Display name for the reply-to e-mail address

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

$to (mixed) − Comma-delimited string or an array of e-mail addresses

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

$cc (mixed) − Comma-delimited string or an array of e-mail addresses

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

$bcc (mixed) − Comma-delimited string or an array of e-mail addresses

$limit (int) − Maximum number of e-mails to send per batch

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

$subject (string) − E-mail subject line

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

$body (string) − E-mail message body

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

$str (string) − Alternative e-mail message body

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

$header (string) − Header name

$value (string) − Header value

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

$clear_attachments (bool) – Whether or not to clear attachments

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

$auto_clear (bool) − Whether to clear message data automatically

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

$filename (string) − File name

$disposition (string) − ‘disposition’ of the attachment. Most email clients make their own decision regardless of the MIME specification used here. iana

$newname (string) − Custom file name to use in the e-mail

$mime (string) − MIME type to use (useful for buffered data)

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

$filename (string) − Existing attachment filename

Attachment Content-ID or FALSE if not found string

发送电子邮件

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

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

加载库之后,只需执行以下功能即可设置发送电子邮件所需的元素。在 从() 函数用来设置-从正在发送的电子邮件,其中并 以() 函数用于- 谁被发送的电子邮件。所述 主体()消息() 函数是用来设置电子邮件的主题和消息。

$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();

创建一个控制器文件 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