- 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