alvlapo / phalcon-mailer

SwiftMailer和视图组件的Phalcon框架邮件包装器

1.0.0 2018-10-01 07:34 UTC

This package is auto-updated.

Last update: 2024-09-19 10:10:35 UTC


README

SwiftMailer邮件器的Phalcon框架包装器

用法

配置

Swift Mailer中有多种传输类型,您创建一个传输,用它来创建管理器,然后您使用管理器来发送邮件。

$transport = new \Swift_SendmailTransport();
$view = \Phalcon\Di::getDefault()->getShared('view');

$mailer = new \Rotoscoping\Phalcon\Manager($transport, $view);

// Add mailer to container (optional)
\Phalcon\Di::getDefault()->setShared('mailer', $mailer);

发送电子邮件

// Get mailer from DI
$mailer = \Phalcon\Di::getDefault()->get('mailer');

// Compose mail
$mailer
  ->to('test@test.com')
  ->subject('Simple subject')
  ->text('Simple text part message')
  ->send();
  
// Fin

配置发件人

有两种方法来配置发件人。

首先,您可以在Mail类的build方法中使用from方法

// You must initialize and add Manager instance to DI as 'mailer' service (see above)
// or extend Mail class and owerride method getMailerInstance()

$mail = new Mail();
$mail
  ->from('support@example.com', 'Support team')
  ->to('test@test.com')
  ->subject('Simple subject')
  ->text('Simple text part message');
  
$mail->send();

或者,您可以通过Manager实例中的草稿邮件指定一个全局的“from”地址。该地址将默认用于所有您的邮件

$defaultMail = new Mail();
$defaultMail
  ->from('support@example.com', 'Support team');

// Get mailer from DI (see initializatio section)
$mailer = \Phalcon\Di::getDefault()->get('mailer');

$mailer->setDraftMail($defaultMail);

$mail = new Mail();
$mail
  ->to('test@test.com')
  ->subject('Simple subject')
  ->text('Simple text part message');
  
$mail->send();

示例

从模板发送消息

// Initialize manager with mail and view services from DI
$mailer = new \Rotoscoping\Phalcon\Manager('mail', 'view');

// template email_confirmation.phtml in your viewDir
$mailer->send(
  // view path
  'email_confirmation',
  // Swift params
  [
    'subject' => 'Example email confirmation',
    'from' => 'no-reply@example.com',
    'to' => 'user@mail.com'
  ],
  // View params
  [ 
    'username' => 'User Name',
    'token' => 'aq1sw2de3'
  ]
);

通过魔法方法调用从模板发送消息

// word of the send from the beginning and the template filename in camelCase notation
$mailer->sendEmailConfirmation(
  [
    'subject' => 'Example email confirmation',
    'from' => 'no-reply@example.com',
    'to' => 'user@mail.com'
  ],
  [
    'username' => 'User Name',
    'token' => 'aq1sw2de3'
  ]
);

使用可邮寄对象发送

使用可邮寄类比上面基本用法示例要优雅得多。在可邮寄类中构建邮件可以清理控制器和路由,使事情看起来更整洁,更不杂乱,并且使事情更容易管理。

所有可邮寄类的配置都是在compose方法中完成的。在这个方法中,您可以调用各种方法,如fromsubjectviewattach来配置电子邮件的展示和交付。

可邮寄类需要扩展基础Rotoscoping\Phalcon\Mailer\Mailable类;

灵感来源于