semhoun/slim-mailer

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

1.0.0 2019-10-03 13:25 UTC

This package is auto-updated.

Last update: 2024-09-29 05:28:47 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads

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

邮件类将大大整理您的控制器方法或路由,并使发送电子邮件变得轻松。

许可证

许可协议:MIT。适用于私人或商业项目,完全免费。

源自 https://github.com/andrewdyer/slim3-mailer

安装

composer require semhoun/slim-mailer

使用

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

$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
	'mailer' => function (ContainerInterface $container) {
            $settings = $container->get('settings');
            $view = $container->get('view');
			$mailer = new \Semhoun\Mailer\Mailer($view, [
                '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();

支持选项

发送电子邮件(基本示例)

$app->get('/email', function (Request $request, Response $response, $args)  use($app) {
	$user = new stdClass;
    $user->name = 'Paul Muaddib';
    $user->email = 'paul.muaddib@mail.com';
    
    $container = $app->getContainer();
    $container->get('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>

使用邮件类发送

使用邮件类比上面的基本用法示例更加优雅。在邮件类中构建邮件可以整理控制器和路由,使事情看起来更加整洁和清晰,同时也使事物更容易管理。

邮件类需要扩展基本类 Semhoun\Mailer\Mailable`;

use Semhoun\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('/email', function (Request $request, Response $response, $args)  use($app) {
	$user = new stdClass;
    $user->name = 'Paul Muaddib';
    $user->email = 'paul.muaddib@mail.com';
    
    $container = $app->getContainer();
    $container->get('mailer')->->setTo($user->email, $user->name)->sendMessage(new WelcomeMailable($user));
    
    $response->getBody()->write('Mail sent!');
    
    return $response;
});

方法

有用链接