状态 草稿
Todo fuller description of driver (when to use one over another)
官方最后更新时间 2009/02/05 04:44

Email 辅助函数

Email 辅助函数实际是运行在 Swift email 类的基础上。如果你想使得辅助函数正常工作必需有 Swiftmailer 库放置在 vendor 文件夹下面,此目录的结构必须有:

 vendor
  +- swift
  |    +- Swift
  |        |  ...
  |        |  ...
  |    -- Swift.php
  |    -- EasySwift.php

配置

配置 swiftmailer 类的配置文件存放在 application/config/email.php,如果没有请从 system/config 复制一份(详细请看级联系统):

// 可用驱动:native,sendmail,smtp 
$config['driver'] = 'native'; 
 
// Driver 选项: 
$config['options'] = NULL;

驱动

config['driver'] 设置 SwiftMailer 驱动。可用驱动:

  • native
  • sendmail
  • smtp

驱动选项

$config['options'] 包含驱动指定参数。

  • smtp: hostname(主机名),port(端口),username(用户名),password(密码),encryption(加密)
// 标准 smtp 连接
$config['options'] = array('hostname'=>'yourhostname', 'port'=>'25', 'username'=>'yourusername', 'password'=>'yourpassword', 'encryption' => 'tls');
 
// 安全 SMTP 连接
  • sendmail: 指定路径或留空让 Swift 自动寻找 sendmail 路径
$config['options'] = '/path/to/sendmail';

用法实例

发生基本 Email

发送基本 HTML 格式的 Email,可以参考下面的代码:

$to      = 'to@example.com';  // 地址也可以使用数组形式:array('to@example.com', 'Name')
$from    = 'from@example.com';
$subject = 'This is an example subject';
$message = 'This is an <strong>example</strong> message';
 
email::send($to, $from, $subject, $message, TRUE);

高级使用

如果你想使用高级方式你需要使用 Swift mailer 方法。下面的代码功能实现了如何添加附件和多接收人发送:

$from = 'from@example.com';
$subject = 'Backup: ' . date("d/m/Y");
$message = 'This is the <b>backup</b> for ' . date("d/m/Y");
 
// 使用 connect() 方法在 email 配置文件使用参数设置来创建连接
$swift = email::connect();
 
// 构建接收人清单
$recipients = new Swift_RecipientList;
$recipients->addTo('to1@example.com');
$recipients->addTo('to2@example.com');
 
// 创建 HTML 格式信息
$message = new Swift_Message($subject, $message, TRUE);
 
// 附件
$swiftfile = new Swift_File('/backups/dump-' . date("d-m-Y") . '.tar.gz');
$attachment = new Swift_Message_Attachment($swiftfile);
$message->attach($attachment);
 
if ($swift->send($message, $recipients, $from))
{
  // 成功
}
else
{
  // 失败
}
 
// 断开连接
$swift->disconnect();

方法

connect()

'connect' 创建 SwiftMailer 接口来驱动和设置配置文件中的参数。

send()

'send' 使用指定信息发送 e-mail,其参数:

  • [string/array] 接收人 Email(和姓名),或者数组方式填写接收人,抄送,密送地址
  • [string/array] 发送人 Email(和姓名)
  • [string] 信息标题
  • [string] 信息内容
  • [boolean] 是否为 HTML 格式发送(默认为 false)
  • 返回 [integer] 发送 Email 的数量
helpers/email.txt · 最后更改: 2009/02/05 17:44 由 icyleaf