| 
 | 
 
 
发表于 2009-3-4 10:32:45
|
显示全部楼层
 
 
 
 本帖最后由 doutu 于 2009-4-1 00: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 驱动。可用驱动:  
 
驱动选项$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,可以参考下面的代码:  
PHP复制代码   
       $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 方法。下面的代码功能实现了如何添加附件和多接收人发送: 
PHP复制代码  
$from = 'from@example.com';
$subject = 'Backup: ' . [url =http ://www.php.net/date]date[/url]("d/m/Y");
$message = 'This is the <b>backup</b> for ' . [url =http ://www.php.net/date]date[/url]("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 的数量
   |   
 
 
 
 |