adrianorsouza/codeigniter-phpmailer

由PHPMailer库支持的CodeIgniter邮件插件

v0.1.5 2019-04-03 23:39 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:03:12 UTC


README

一个简单的CodeIgniter邮件类,通过强大的PHPMailer库连接您的应用程序。

Latest Stable Version Total Downloads Latest Unstable Version License

在CodeIgniter v2.1.4 / v3.0-dev和PHPMailer版本5.2.7上进行了测试

通过Composer安装

通过Composer获取此插件是快速入门。

此插件使用Composer进行安装并依赖PHPMailer。如果您还没有安装,请先安装Composer

并且可以通过Composer/Packagist获得。一旦您在环境中配置了Composer,请在命令行中运行以下命令

  $ composer require "adrianorsouza/codeigniter-phpmailer:0.1.*"

此命令将在composer.json中写入内容,下载并将此项目文件和PHPMailer依赖项放入您的vendor文件夹。

如果您尚未包含composer自动加载,您必须在index.php页面或运行此插件的地方的顶部进行此操作。

require_once __DIR__.'/vendor/autoload.php';

这就完成了。您可以在CodeIgniter应用程序的任何地方发送电子邮件。

要创建Mail()类的实例

 $mail = new Mail();

或者手动安装

如果您不懂Composer,请按照以下说明操作

  • 下载此zip文件,并将其解压到您的application/目录中。

  • 下载PHPMailer文件依赖项因为它们不包括在此包中

    • PHPMailerAutoload.php
    • class.smtp.php
    • class.phpmailer.php

将它们放入您的third_party文件夹或what/you/want,只要您在调用类Mail()的地方包含PHPMailer自动加载器。

要加载类Mail(),请使用相同的CodeIgniter超级对象

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

这就完成了。

配置

获取此插件后,您必须设置mail_config.php文件,该文件包含所有必须放置在您的application/config/文件夹中的邮件服务器配置。

为了能够从您的本地开发服务器或生产服务器发送电子邮件,您必须提供有效的SMTP账户认证。

因此,在此配置文件中,您设置SMTP服务器和密码、登录名、发件人等...

要设置用于发送电子邮件的Gmail SMTP账户,您必须将配置mail_smtp_secure设置为TLS

要将HTML模板文件放在您的视图中发送任何消息,请将其放置在您的视图文件夹中。默认文件夹是views/templates/email,如果您想更改它,请在此设置mail_config.php,只要它位于视图文件夹下即可。

示例mail_config.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// Mailer Configuration
$config['mail_mailer']          = 'PHPMailer';
$config['mail_debug']           = 0; // default: 0, debugging: 2, 'local'
$config['mail_debug_output']    = 'html';
$config['mail_smtp_auth']       = true;
$config['mail_smtp_secure']     = ''; // default: '' | tls | ssl |
$config['mail_charset']         = 'utf-8';

// Templates Path and optional config
$config['mail_template_folder'] = 'templates/email';
$config['mail_template_options'] = array(
                                       'SITE_NAME' => 'Codeigniter Mail Plugin',
                                       'SITE_LOGO' => 'https:///images/logo.jpg',
                                       'BASE_URL'  => 'https://',
                                    );
// Server Configuration
$config['mail_smtp']            = 'smtp.example.com';
$config['mail_port']            = 587; // for gmail default 587 with tls
$config['mail_user']            = 'someone@example.com';
$config['mail_pass']            = 'secret';

// Mailer config Sender/Receiver Addresses
$config['mail_admin_mail']      = 'someone@example.com';
$config['mail_admin_name']      = 'WebMaster';

$config['mail_from_mail']       = 'someone@example.com';
$config['mail_from_name']       = 'Site Name';

$config['mail_replyto_mail']    = 'someone@example.com';
$config['mail_replyto_name']    = 'Reply to Name';

// BCC and CC Email Addresses
$config['mail_bcc']             = 'someone@example.com';
$config['mail_cc']              = 'someone@example.com';

// BCC and CC enable config, default disabled;
$config['mail_setBcc']          = false;
$config['mail_setCc']           = false;


/* End of file mail_config.php */
/* Location: ./application/config/mail_config.php */

用法

使用字符串作为HTML正文发送基本的电子邮件消息。

$data = '<h2>Sample Basic</h2>
         <hr>
         <p>This is a simple basic mail message in <strong>HTML</strong> string format</p>
         <p>Lorem ipsum dolor sit amharum<br /> quod deserunt id dolores.</p>';

$mail = new Mail();
$mail->setMailBody($data);
$mail->sendMail('Awesome Subject', 'someone@example.com');

使用外部HTML模板发送电子邮件消息

$data = null;
$template_html = 'sample-1.html'; //views/templates/mail/

$mail = new Mail();
$mail->setMailBody($data, $template_html);
$mail->sendMail('Awesome Subject', 'someone@example.com');

使用关联数组以及外部HTML模板发送电子邮件消息

创建一个HTML模板文件,并将其命名为sample-2.html

<table>
  <tr>
    <td style="width:640px; padding:20px;">
      <h2><img src="{SITE_LOGO}" width="49" height="48" alt="{SITE_NAME}" /> {SITE_NAME}</h2>
    <h3>Hello {NAME}</h3>
      <h3>Your email address is: {EMAIL}</h3>
      <p>
        {MESSAGE}
      </p>
      <hr>
      <span>
        {DATE}
      </span>
      <hr>
      <p>
        {SMALL_TEXT}
      </p>
    </td>
  </tr>
</table>

然后设置一个新的邮件消息。

$data = array(
        "NAME"       => 'Juliet & Romeo',
        "EMAIL"      => 'some_email@example.com',
        "MESSAGE"    => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.',
        "DATE"       => date('Y-m-d H:i:s'),
        "SMALL_TEXT" => 'Sapiente, fugiat consequatur illo eaque ipsam expedita sint itaque',
        );

$template_html = 'sample-2.html'; //views/templates/mail/

$mail = new Mail();
$mail->setMailBody($data, $template_html);
$mail->sendMail('Awesome Subject', 'someone@example.com');

更多示例可以在application/controllers文件夹中的Sample_controller.php文件中找到。

此类可以通过传递数组作为正文内容、外部模板HTML/TEXT文件或字符串来发送电子邮件消息。

如果您没有在sendMail($Subject, $to);方法中传递电子邮件地址作为$to参数,则默认情况下$to将使用您的配置mail_replyto_mail值。

您可以将来自 mail_config.php 的任何配置传递给类构造函数,作为值数组

$config = array(
            'mail_bcc'=>'my_email@example.com',
            'mail_setBcc'=>true
          );

$mail = new Mail($config);

这将启用 BCC 并将副本作为 BCC 发送到地址 my_email@example.com

注意事项

  1. 如果您不在 Composer 中,而不是创建新的实例类,例如:$mail = new Mail();,只需用以下方式正常加载类:$this->load->library('mail');,并使用超级对象 $this->mail->sendMail();

  2. 默认情况下,PHPMailer 以 HTML 格式发送电子邮件消息,并为不支持 HTML 的电子邮件客户端提供替代的纯文本。但默认情况下,纯文本内容是从您的 HTML 主体中提取并剥离成文本的。有时,根据您的 HTML 结构,您的纯文本格式可能无法阅读,所以如果您想为文本-纯文本格式指定特定模板,只需创建一个与 HTML 模板同名并以 .txt 扩展名结尾的 .txt 文件,只写入您需要的文本,邮件插件将找到它并使用其内容。

  3. 请随意根据您的需求自定义此插件。

错误

您发现了一个错误吗?请打开一个 新问题

作者

Adriano Rosa

有关 PHPMailer 的进一步阅读

https://github.com/Synchro/PHPMailer

有关 CodeIgniter 的进一步阅读

http://ellislab.com/codeigniter