hete/mail

为 Kohana 框架提供的多后端邮件发送器

安装次数: 20

依赖关系: 0

建议者: 0

安全性: 0

星标: 5

关注者: 9

分支: 2

类型:kohana-module

dev-3.3/master 2015-08-07 18:19 UTC

README

为 Kohana 框架提供的简单邮件发送器。

支持以下发送方式

旨在统一邮件系统接口,使您能够独立于服务器或组织中的库部署应用程序。

PEAR Mail 模块的 SMTP 发送者使用旧 PHP4 代码,会抛出严格的警告。如果导入,它将自动禁用 E_STRICT。建议在 PRODUCTION 环境中使用它,并使用备用发送者进行测试。

基本用法

Mailer::factory()
    ->headers('Content-Type', 'text/html')
    // ->headers('Content-Type', 'text/html; charset=utf-8') to specify UTF-8 character encoding
    ->subject('Hey :username!')
    ->body(View::factory('some_template'))
    ->param(':username', 'John McGuire')
    ->send(array('John McGuire' => 'foo@example.com' ));

使用 Mail_Sender::param 函数来替换正文和主题。如果您使用 View 作为正文,通过 View::factory 传递变量会更加方便。

通过 headers 函数设置标题。如果值是 array,则标题将被解析为 RFC 收件人列表。

$mailer->headers('Bcc', array('johndoe@example.com' => 'John Doe'));

CcBccReply-To 等定义了别名,以便于使用。

$mailer->bcc('johndoe@example.com');

附件

可以使用 Mail_Sender::attachment 在邮件中附加附件内容。您可以指定针对该附件特定的标题数组。

带有附件的邮件将自动转换为多部分格式。

Mailer::factory()
    ->subject('Got a new cat picture for you.')
    ->attachment(file_get_contents('cat.png'), array(
        'Content-Type' => 'image/png',
        'Content-Disposition' => 'attachment; filename=cat.png')
    ->send('foo@example.com');

接收者

接收者必须符合以下格式

简单的电子邮件

$receiver = "john@example.com"; # a simple email
$receivers = array("john@example.com", "james@example.com"); # a list of emails
$receivers = array("john@example.com" => "John Doe # an associative array
$receivers = array("john@example.com", "james@example.com" => "James Doe"); # a mixed array

与 ORM 非常方便

$receivers = ORM::factory('user')
    ->find_all()
    ->as_array('email', 'full_name');

Mailer::factory()
    ->reply_to('noreply@example.com')
    ->body('Hey guys!')
    ->send($receivers);

发送大量邮件

您可以使用 register_shutdown_function 发送大量邮件

register_shutdown_function(array($mailer, 'send'), $users);

用户得到响应后,邮件将被发送。

生成 Message-ID

有一个基于 Matt Curtin & Jamie Zawinski 建议 的消息 ID 实现。它生成安全的标识符,以便进行线程和其他复杂的邮件处理。

Mailer::factory()
    ->in_reply_to(Mailer::message_id())
    ->body('Hey Foo, long time no see!')
    ->send('foo@example.com')

测试邮件

该模块提供了一个模拟发送者以进行高效的测试。邮件被推送到 Mail_Sender_Mock::$history 栈中,这样您可以检索它们并测试其内容。

public function testMail() 
{
    // self-request to send a mail
    Request::factory('send')->execute();

    $mail = array_pop(Mail_Sender_Mock::$history);
    
    $this->assertEquals('text/html', $mail->headers('Content-Type'));
    $this->assertContains('foo@example.com', $mail->to);
    
    $this->assertTag(array('tag' => 'a', 'attributes' => array('href' => 'http://example.com')), $mail->body());
}