| 状态 | 草稿 |
|---|---|
| Todo | fuller description of driver (when to use one over another) |
| 官方最后更新时间 | 2009/02/05 04:44 |
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 驱动。可用驱动:
$config['options'] 包含驱动指定参数。
// 标准 smtp 连接 $config['options'] = array('hostname'=>'yourhostname', 'port'=>'25', 'username'=>'yourusername', 'password'=>'yourpassword', 'encryption' => 'tls'); // 安全 SMTP 连接
$config['options'] = '/path/to/sendmail';
发送基本 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' 创建 SwiftMailer 接口来驱动和设置配置文件中的参数。
'send' 使用指定信息发送 e-mail,其参数: