andrewdyer/slim3-mailer

使用Twig和Swift Mailer为Slim框架提供邮件支持。

1.2.2 2024-05-21 13:10 UTC

This package is auto-updated.

Last update: 2024-09-21 14:01:26 UTC


README

Total Downloads Latest Stable Version License

使用Twig和Swift Mailer为Slim框架提供邮件支持。可邮寄类将大大整理您的控制器方法或路由,并使发送邮件变得轻松。

许可

在MIT许可下。完全免费用于私人或商业项目。

安装

composer require andrewdyer/slim3-mailer

用法

Anddye\Mailer\Mailer的新实例附加到您的应用程序容器中,以便可以在任何需要的地方访问。 Mailer接受两个参数;一个Slim\Views\Twig的实例和一个可选的SMTP设置数组。

$app = new Slim\App;
    
$container = $app->getContainer();
       
$container['mailer'] = function($container) {
    $twig = $container['view'];
    $mailer = new Anddye\Mailer\Mailer($twig, [
        'host'      => '',  // SMTP Host
        'port'      => '',  // SMTP Port
        'username'  => '',  // SMTP Username
        'password'  => '',  // SMTP Password
        'protocol'  => ''   // SSL or TLS
    ]);
        
    // Set the details of the default sender
    $mailer->setDefaultFrom('no-reply@mail.com', 'Webmaster');
    
    return $mailer;
};
    
$app->run();

如果您的应用程序尚未使用Twig视图,您还需要将其附加到容器中。

$container['view'] = function ($container) {
    $view = new Slim\Views\Twig(__DIR__ . '/../resources/views');
    $basePath = rtrim(str_ireplace('index.php', '', $container['request']->getUri()->getBasePath()), '/');
    $view->addExtension(new Slim\Views\TwigExtension($container['router'], $basePath));
    
    return $view;
};

支持选项

发送邮件(基本示例)

$app->get('/', function ($request, $response) use($container) {
    $user = new stdClass;
    $user->name = 'John Doe';
    $user->email = 'johndoe@mail.com';
    
    $container['mailer']->sendMessage('emails/welcome.html.twig', ['user' => $user], function($message) use($user) {
        $message->setTo($user->email, $user->name);
        $message->setSubject('Welcome to the Team!');
    });
    
    $response->getBody()->write('Mail sent!');
    
    return $response;
});

welcome.html.twig

<h1>Hello {{ user.name }}</h1>
    
<p>Welcome to the Team!</p>
    
<p>Love, Admin</p>

使用可邮寄对象发送

使用可邮寄类比上述基本用法示例更加优雅。在可邮寄类中构建邮件可以清理控制器和路由,使事物看起来更加整洁,更加不杂乱,同时使事物更加易于管理。

可邮寄类需要扩展基本Anddye\Mailer\Mailable类;

use Anddye\Mailer\Mailable;

class WelcomeMailable extends Mailable
{
    
    protected $user;
    
    public function __construct($user)
    {
        $this->user = $user;
    }
    
    public function build()
    {
        $this->setSubject('Welcome to the Team!');
        $this->setView('emails/welcome.html.twig', [
            'user' => $this->user
        ]);
        
        return $this;
    }
    
}

现在在您的控制器或路由中,您设置收件人的地址和姓名,只需将可邮寄类的新实例传递给sendMessage方法作为单个参数;

$app->get('/', function ($request, $response) use($container) {
    $user = new stdClass;
    $user->name = 'John Doe';
    $user->email = 'johndoe@mail.com';
    
    $container['mailer']->setTo($user->email, $user->name)->sendMessage(new WelcomeMailable($user));
     
    $response->getBody()->write('Mail sent!');
    
    return $response;
});

方法

支持

如果您对这个库有任何一般性问题,请随时通过Twitter联系我。

如果您认为您发现了问题,请使用问题跟踪器报告,或者更好的是,分支仓库并提交一个拉取请求。

如果您正在使用此包,我非常想听听您的想法!

有用链接