如何使用以下方式发送电子邮件 Code点火器
⚡ 智能摘要
CodeIgniter 的邮件发送功能依赖于框架内置的邮件库,通过 SMTP 或本地邮件服务器发送邮件。本示例创建了一个邮件配置文件、一个联系表单视图以及一个读取表单输入并发送邮件的控制器。

电子邮件在网络应用程序中至关重要。用户注册时,我们可能需要向其发送邮件以验证邮箱地址,并允许用户确认订阅。我们还会使用电子邮件来重置用户忘记的密码,以及向客户发送发票和收据。 CodeIgniter 使我们能够非常轻松地使用各种选项从我们的应用程序发送电子邮件。
CodeIgniter 内置了一个电子邮件库,我们可以在发送电子邮件时使用它。
CodeIgniter 电子邮件配置
我们需要一个集中管理电子邮件设置的地方。 CodeIgniter 没有自带电子邮件配置文件,所以我们需要自己创建一个。
在目录 application/config 中创建文件 email.php
将以下代码添加到 email.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); $config = array( 'protocol' => 'smtp', // 'mail', 'sendmail', or 'smtp' 'smtp_host' => 'smtp.example.com', 'smtp_port' => 465, 'smtp_user' => 'no-reply@example.com', 'smtp_pass' => '12345!', 'smtp_crypto' => 'ssl', //can be 'ssl' or 'tls' for example 'mailtype' => 'text', //plaintext 'text' mails or 'html' 'smtp_timeout' => '4', //in seconds 'charset' => 'iso-8859-1', 'wordwrap' => TRUE );
这里,
- 'protocol' => 'smtp',指定发送电子邮件时要使用的协议。这可以是 Gmail SMTP 设置或来自您主机的 SMTP 设置
- 'smtp_host' => 'smtp.example.com',指定 SMTP 主机。例如,如果您想使用 Gmail 那么你就会得到类似 smtp.gmail.com 这样的域名。
- 'smtp_port' => 465,指定 SMTP 主机上已配置用于 SMTP 邮件的开放端口。
- 'smtp_user' => 'no-reply@example.com',发送电子邮件时将用作发件人的电子邮件地址。这应该是服务器上存在的有效电子邮件地址
- 'smtp_pass' => '12345!',指定 SMTP 用户邮箱的密码
- 'smtp_crypto' => 'ssl',指定要使用的加密方法,例如 ssl、tls 等。
- `mailtype` => 'text',设置邮件类型。可以是纯文本或 HTML,具体取决于您的需求。
- 'smtp_timeout' => '4',指定在抛出超时异常之前尝试连接主机时应经过的时间(以秒为单位)。
- 'charset' => 'iso-8859-1',定义发送邮件时使用的字符集。
- 'wordwrap' => TRUE,如果设置为 TRUE,则启用自动换行;如果设置为 FALSE,则禁用自动换行。
注意::为了发送电子邮件,您应该提供有效的配置参数。虚假参数将无法发送电子邮件。
CodeIgniter 电子邮件视图
在本节中,我们将创建向收件人发送电子邮件的视图。
在 application/views 中创建新目录 email
在 application/views/email 目录下创建一个名为 contact.php 的新文件。
将以下代码添加到 application/views/email/contact.php
<!DOCTYPE html> <html> <head> <title>CodeIgniter Send Email</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div> <h3>Use the form below to send email</h3> <form method="post" action="<?=base_url(0)?>" enctype="multipart/form-data"> <input type="email" id="to" name="to" placeholder="Receiver Email"> <br><br> <input type="text" id="subject" name="subject" placeholder="Subject"> <br><br> <textarea rows="6" id="message" name="message" placeholder="Type your message here"></textarea> <br><br> <input type="submit" value="Send Email" /> </form> </div> </body> </html>
这里,
- 我们有一个基本的 HTML 表单,可以接收电子邮件地址、主题和正文,然后将参数传递给电子邮件路由。
CodeIgniter 电子邮件控制器
现在让我们创建负责发送电子邮件的控制器。
在 application/controllers/EmailController.php 中创建一个新文件 EmailController.php
将以下代码添加到 EmailController.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class EmailController extends CI_Controller { public function __construct() { parent:: __construct(); $this->load->helper('url'); } public function index() { $this->load->view('email/contact'); } function send() { $this->load->config('email'); $this->load->library('email'); $from = $this->config->item('smtp_user'); $to = $this->input->post('to'); $subject = $this->input->post('subject'); $message = $this->input->post('message'); $this->email->set_newline("\r\n"); $this->email->from($from); $this->email->to($to); $this->email->subject($subject); $this->email->message($message); if ($this->email->send()) { echo 'Your Email has successfully been sent.'; } else { show_error($this->email->print_debugger()); } } }
这里,
- class EmailController extends CI_Controller {…} 定义了我们的邮件控制器,它继承自父类。 Code点火控制器。
- public function __construct() {…} 定义调用父构造函数方法的子构造函数。
- public function index() {…} 定义显示联系表单的 index 方法
- function send() {…} 定义发送电子邮件的方法
- $this->load->config('email'); 加载电子邮件配置设置
- $this->load->library('email'); 加载电子邮件库
- $from = $this->config->item('smtp_user'); 从我们定义的电子邮件配置文件中获取发件人 ID。
- $to = $this->input->post('to'); 从提交的表单中获取 to 值
- `$subject = $this->input->post('subject');` 设置表单中的电子邮件主题
- $message = $this->input->post('message'); 从表单设置电子邮件消息
- $this->email->set_newline(“\r\n”); 定义电子邮件的换行符
- $this->email->from($from); 设置发件人电子邮件地址
- $this->email->to($to);设置收件人电子邮件地址
- $this->email->subject($subject); 设置电子邮件主题
- $this->email->message($message); 设置电子邮件消息
- if ($this->email->send()) {…} 尝试发送电子邮件。如果电子邮件发送成功,则显示消息“您的电子邮件已成功发送”,否则打印调试信息,说明可能出错的原因。
现在让我们来定义电子邮件路由。
电子邮件路线
将以下路由添加到 application/config/routes.php
$route['send-email'] = 'EmailController'; $route['email'] = 'EmailController/send';
现在我们可以在网页浏览器中加载联系表单了。
让我们启动内置的PHP服务器。
打开终端或命令行,并浏览到应用程序的根目录。在我的示例中,根目录位于 C:\Sites\ci-app 驱动器。
cd C:\Sites\ci-app
使用以下命令启动服务器
php -S localhost:3000
加载以下内容 URL 在您的网络浏览器中: http://localhost:3000/send-email
你应该能够看到以下表格
输入收件人邮箱地址、邮件主题和邮件正文,然后点击“发送邮件”。如果您的邮件配置正确,您应该会看到成功提示信息。
如何在电子邮件中发送带有附件的 HTML 电子邮件 Code点火器
同一个邮件库可以发送格式丰富的邮件和文件。只需稍作修改,即可将上面的纯文本示例转换为带有附件的 HTML 邮件。
$this->email->set_mailtype('html'); $this->email->message('<h3>Welcome</h3><p>Thanks for joining us.</p>'); $this->email->attach('/path/to/report.pdf');
- 设置邮件类型('html'): 告诉库正文包含 HTML,以便标题和链接等标签在收件人的收件箱中呈现。
- 附(): 将服务器路径中的文件添加到消息中;在 send() 之前,对每个文件调用一次。
请记住,HTML 电子邮件仍应包含纯文本替代方案,并且严格的邮件服务器可能会拒绝大型附件。

