- CodeIgniter 教程
- CodeIgniter - 首頁
- CodeIgniter - 概述
- CodeIgniter - 安裝 CodeIgniter
- CodeIgniter - 應用程式架構
- CodeIgniter - MVC 框架
- CodeIgniter - 基本概念
- CodeIgniter - 配置
- CodeIgniter - 使用資料庫
- CodeIgniter - 庫
- CodeIgniter - 錯誤處理
- CodeIgniter - 檔案上傳
- CodeIgniter - 傳送郵件
- CodeIgniter - 表單驗證
- CodeIgniter - 會話管理
- CodeIgniter - Flashdata
- CodeIgniter - Tempdata
- CodeIgniter - Cookie 管理
- CodeIgniter - 常用函式
- CodeIgniter - 頁面快取
- CodeIgniter - 頁面重定向
- CodeIgniter - 應用程式分析
- CodeIgniter - 基準測試
- CodeIgniter - 新增 JS 和 CSS
- CodeIgniter - 國際化
- CodeIgniter - 安全性
- CodeIgniter 有用資源
- CodeIgniter - 快速指南
- CodeIgniter - 有用資源
- CodeIgniter - 討論
CodeIgniter - 傳送郵件
在 CodeIgniter 中傳送電子郵件非常容易。您還可以配置 CodeIgniter 中有關電子郵件的偏好設定。CodeIgniter 提供以下發送電子郵件的功能:
- 多種協議 - 郵件、Sendmail 和 SMTP
- SMTP 的 TLS 和 SSL 加密
- 多個收件人
- 抄送和密送
- HTML 或純文字電子郵件
- 附件
- 自動換行
- 優先順序
- BCC 批處理模式,允許將大型電子郵件列表拆分為小的 BCC 批次。
- 電子郵件除錯工具
電子郵件類包含以下函式,簡化了傳送電子郵件的任務。
| 序號 | 語法 | 引數 | 返回值 | 返回值型別 |
|---|---|---|---|---|
| 1 | from($from[, $name = ''[, $return_path = NULL]]) |
$from (字串) - “發件人”電子郵件地址 $name (字串) - “發件人”顯示名稱 $return_path (字串) - 可選的電子郵件地址,用於將未送達的電子郵件重定向到該地址 |
CI_Email 例項(方法鏈) | CI_Email |
| 2 | reply_to($replyto[, $name = '']) |
$replyto (字串) - 回覆的電子郵件地址 $name (字串) - 回覆電子郵件地址的顯示名稱 |
CI_Email 例項(方法鏈) | CI_Email |
| 2 | to($to) |
$to (混合) - 以逗號分隔的字串或電子郵件地址陣列 |
CI_Email 例項(方法鏈) | CI_Email |
| 3 | cc($cc) |
$cc (混合) - 以逗號分隔的字串或電子郵件地址陣列 |
CI_Email 例項(方法鏈) | CI_Email |
| 4 | bcc($bcc[, $limit = '']) |
$bcc (混合) - 以逗號分隔的字串或電子郵件地址陣列 $limit (整數) - 每個批次傳送的最大電子郵件數量 |
CI_Email 例項(方法鏈) | CI_Email |
| 5 | subject($subject) |
$subject (字串) - 電子郵件主題行 |
CI_Email 例項(方法鏈) | CI_Email |
| 6 | message($body) |
$body (字串) - 電子郵件正文 |
CI_Email 例項(方法鏈) | CI_Email |
| 7 | set_alt_message($str) |
$str (字串) - 備用電子郵件正文 |
CI_Email 例項(方法鏈) | CI_Email |
| 8 | set_header($header, $value) |
$header (字串) - 標頭名稱 $value (字串) - 標頭值 |
CI_Email 例項(方法鏈) | CI_Email |
| 9 | clear([$clear_attachments = FALSE]) |
$clear_attachments (布林值) - 是否清除附件 |
CI_Email 例項(方法鏈) | CI_Email |
| 10 | send([$auto_clear = TRUE]) |
$auto_clear (布林值) - 是否自動清除郵件資料 |
CI_Email 例項(方法鏈) | CI_Email |
| 11 | attach($filename[, $disposition = ''[, $newname = NULL[, $mime = '']]]) |
$filename (字串) - 檔名 $disposition (字串) - 附件的“處置方式”。大多數電子郵件客戶端都會根據此處使用的 MIME 規範自行決定。iana $newname (字串) - 在電子郵件中使用的自定義檔名 $mime (字串) - 要使用的 MIME 型別(對於緩衝資料很有用) |
CI_Email 例項(方法鏈) | CI_Email |
| 12 | attachment_cid($filename) |
$filename (字串) - 現有附件檔名 |
附件內容 ID 或如果未找到則為 FALSE | 字串 |
傳送電子郵件
要使用 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();
示例
建立一個控制器檔案 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 替換為您站點的 URL。
http://yoursite.com/index.php/email