依赖倒置原则应用,Lumen基于PHPMailer封装邮件服务
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步便可无缝切换:
- 为某个邮件服务商封装服务类,继承EmailInterface接口,并遵循规则,实现对应的方法
- 替换EmailServiceProvider的register方法中的new一行代码为新的服务类
相关文章
- Recyclerview_helper多功能封装,让你的应用更加自如
- JDBC辅助类封装 及应用
- canvas的代码封装
- 126.AJAX封装_请求参数
- jquery是如何封装的
- 规则引擎drools封装
- 小程序api封装(方法一)
- Recyclerview_helper多功能封装,让你的应用更加自如
- vue_music:封装scroll.vue组件
- 封装自用Linux命令
- vue基于promise可以用于浏览器和node.js的网络请求库【axios封装-收藏版】
- Mediapipe – 将Mediapipe handtracking封装成动态链接库dll/so,实现在桌面应用中嵌入手势识别功能
- PermissUtils_getBrightnessPermiss 安卓6.0系统权限申请android.permission.WRITE_SETTINGS封装方法
- js方法封装
- 【FFMpeg视频开发与应用基础】四、调用FFmpeg SDK解析封装格式的视频为音频流和视频流