zl程序教程

您现在的位置是:首页 >  前端

当前栏目

依赖倒置原则应用,Lumen基于PHPMailer封装邮件服务

封装应用依赖服务 基于 邮件 原则 Lumen
2023-09-27 14:25:41 时间

EmailInterface

<?php
namespace App\Services\Email;

interface EmailInterface
{
    public function send($email, $subject, $content, $attachments = []);
}

PhpMailerService

<?php
namespace App\Services\Email;

use App\Services\Util\BLogger;
use PHPMailer\PHPMailer\PHPMailer;

class PhpMailerService implements EmailInterface
{
    protected $mailer;
    protected $SMTPDebug = 2;
    protected $host = 'email.qq.com';
    protected $SMTPAuth = true;
    protected $sendEmail = 'test@qq.com';
    protected $sendName = '发信人';
    protected $password = 'gnehkg83nggelnl';
    protected $SMTPSecure = 'ssl';
    protected $port = 465;

    public function __construct()
    {
        $this->mailer = new PHPMailer();
    }

    public function send($email, $subject, $content, $attachments = [])
    {
        try{
            $this->mailer->SMTPDebug = $this->SMTPDebug;
            $this->mailer->isSMTP();
            $this->mailer->Host = $this->host;
            $this->mailer->SMTPAuth = $this->SMTPAuth;
            $this->mailer->Username = $this->sendEmail;
            $this->mailer->Password = $this->password;
            $this->mailer->SMTPSecure = $this->SMTPSecure;
            $this->mailer->Port = $this->port;

            $this->mailer->setFrom($this->sendEmail, $this->sendName);
            $this->mailer->addAddress($email);

            if(!empty($attachments)){
                foreach ($attachments as $attachment){
                    $this->mailer->addAttachment($attachment);
                }
            }

            $this->mailer->isHTML(true);
            $this->mailer->Subject = $subject;

            $this->mailer->Body = $content;

            $res = $this->mailer->send();

            BLogger::getLogger('email')->info(['email' => $email, 'content' => $content, 'res' => $res]);

            if($res){
                return 1;
            }else{
                return 0;
            }
        }catch (\Exception $e){
            BLogger::getLogger('email')->error($e);
            return 0;
        }
    }

    public function setHost( $host )
    {
        $this->host = $host;
    }

    public function setPassword( $password )
    {
        $this->password = $password;
    }

    public function setSendEmail( $sendEmail )
    {
        $this->sendEmail = $sendEmail;
    }

    public function setSendName( $sendName )
    {
        $this->sendName = $sendName;
    }


}

EmailServiceProvider

<?php

namespace App\Providers;

use App\Services\Email\EmailInterface;
use App\Services\Email\PhpMailerService;
use Illuminate\Support\ServiceProvider;

class EmailServiceProvider extends ServiceProvider
{
    protected $defer = true;

    public function boot()
    {

    }

    public function register()
    {
        $this->app->singleton(EmailInterface::class, function (){
            return new PhpMailerService();
        });
    }

    public function provides()
    {
        return [EmailInterface::class];
    }
}

app.php中注册服务provider

$app->register(App\Providers\EmailServiceProvider::class);

Controller

public function sendEmail(EmailInterface $email)
{
    echo $email->send('1597280205@qq.com', '测试标题', '测试内容');
}

 

如果想替换邮件服务,只需要如下2步便可无缝切换:

  1. 为某个邮件服务商封装服务类,继承EmailInterface接口,并遵循规则,实现对应的方法
  2. 替换EmailServiceProvider的register方法中的new一行代码为新的服务类