電郵管理模組提供了一個全面的解決方案,用於管理和自動化電郵通訊。該模組支持SMTP配置、電郵模板管理、即時發送測試、以及電郵推廣活動,適合需要高效電郵交互的現代Web應用。通過友好的用戶界面和靈活的後端服務,開發者可以輕鬆地整合和使用這些功能,以提高用戶溝通的效率和效果。
支持以下功能和特性:
範例:SMTPMail
<?php
namespace Modules\Edm\Domain\SMTP;
use Bingo\Enums\Code;
use Bingo\Exceptions\BizException;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;
use Modules\Edm\Domain\Email\Logs;
class SMTPMail
{
public function __construct(
protected readonly SMTPConfig $smtpConfig,
protected readonly Logs $mailLogService,
) {
}
public function send($to, $subject, $content, $service)
{
// 使用邮件配置服务获取和设置邮件配置
$config = $this->smtpConfig->getAndSetMailConfig($service);
// 创建邮件日志记录
$logs = $this->mailLogService->createMailLog($to, $service, $subject, $content, $config['from']);
// 使用自定义的邮件驱动来发送邮件
$mailer = Mail::mailer($config['driver']);
// 尝试发送邮件,并处理异常
$this->attemptSendMail($mailer, $to, $subject, $content, $logs);
// 保存邮件发送日志
$this->mailLogService->saveMailLog($logs);
return true;
}
protected function attemptSendMail($mailer, $to, $subject, $content, &$logs)
{
try {
$mailer->to($to)->send($this->buildMailable($subject, $content));
$logs['status'] = 1; // 发送成功
} catch (\Exception $e) {
$logs['status'] = 2; // 发送失败
$this->mailLogService->saveMailLog($logs); // 保存发送失败的日志
BizException::throws(Code::FAILED, '邮件发送失败:'.$e->getMessage());
}
}
protected function buildMailable($subject, $htmlContent)
{
return new class ($subject, $htmlContent) extends Mailable {
public $subject;
public $htmlContent;
public function __construct($subject, $htmlContent)
{
$this->subject = $subject;
$this->htmlContent = $htmlContent;
}
public function build()
{
return $this->subject($this->subject)
->html($this->htmlContent);
}
};
}
}