定制 devlab-studio/laravel-mailer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

devlab-studio/laravel-mailer

最新稳定版本:1.0.13

Composer 安装命令:

composer require devlab-studio/laravel-mailer

包简介

This is my package laravel-mailer

README 文档

README

Devlab Logo

Español

Laravel Mailer

Laravel package for advanced email sending with support for multiple SMTP senders, email logging and attachment management.

Summary

  • Registers a custom channel to send notifications via SMTP configured per sender.
  • Persists sent emails (body, metadata, status) and attachments to the database and storage (storage/app/attachments/...).
  • Allows configuring SMTP senders via an interactive command or using .env and a seeder.

Installation

Install via Composer:

composer require devlab-studio/laravel-mailer

Publish configuration (if applicable) and run migrations:

php artisan vendor:publish --tag=laravel-mailer-config
php artisan migrate

SMTP Configuration (.env)

Before using the package, fill in your SMTP credentials in .env or use the interactive command:

  • MAIL_MAILER=smtp
  • MAIL_HOST=your.smtp.host
  • MAIL_PORT=587
  • MAIL_USERNAME=your@smtp.user
  • MAIL_PASSWORD=secret
  • MAIL_ENCRYPTION=tls # tls, ssl or null
  • MAIL_FROM_ADDRESS=from@example.com
  • MAIL_FROM_NAME="Your Name"

Or run the interactive setup (it will save runtime config and run the seeder):

php artisan laravel-mailer

The command will ask for host, port, protocol, user, password, sender address and name and will run EmailSendersTableSeeder.

Senders Seeder

The EmailSendersTableSeeder inserts a sender into the email_senders table using current mail configuration (normally from .env). Run it manually:

php artisan db:seed --class=\\Devlab\\LaravelMailer\\Database\\Seeders\\EmailSendersTableSeeder

File: database/seeders/EmailSendersTableSeeder.php

Custom channel and send flow

The main channel is CustomMailChannel (src/CustomMail/CustomMailChannel.php) and does the following:

  • Retrieves the recipient from the notifiable. Supports AnonymousNotifiable.
  • Resolves the sender (from) from the message or from config (devlab.MAIL_FROM_ADDRESS).
  • Logs the email in the emails table storing HTML body, subject, to/cc/bcc and metadata.
  • Stores attachments in storage/app/attachments/YYYY/M/D/ and creates records in emails_attachments.
  • Selects which mailer to use by querying email_senders table:
    • If the sender exists, it dynamically creates a mailer custom{ID} with the sender credentials (password decrypted) and uses it.
    • Otherwise it falls back to the default smtp mailer.
  • Sends the message using the selected mailer and updates the email record with status, sent date and errors if any.

Key files:

  • src/CustomMail/CustomMailChannel.php
  • src/Models/Email.php (logging and filters)
  • src/Models/EmailsAttachment.php (attachment metadata)
  • src/Models/EmailSender.php (SMTP senders)

Database structure

The package includes migrations in database/migrations to create:

  • email_senders — SMTP senders and credentials (password encrypted)
  • emails — records of sent emails (body, status, to/cc/bcc, sent_at, etc.)
  • emails_attachments — attachment metadata and storage paths

Check database/migrations for exact columns.

Attachments storage

Physical attachments are copied to storage/app/attachments/{year}/{month}/{day}/ and the path is recorded in the DB. The channel supports:

  • Illuminate\\Mail\\Attachment
  • Illuminate\\Http\\UploadedFile
  • Disk paths (strings)

Usage (quick example)

Inside a notification, implement toCustomMail() and return a Mailable or MailMessage with attachments and rawAttachments when needed. When notifying, the package:

// Notification
public function toCustomMail($notifiable)
{
    $mailable = new \\Illuminate\\Mail\\Mailable();
    // configure view, subject, attachments, etc.
    return $mailable;
}

// Send
$user->notify(new \\App\\Notifications\\MyNotification());

Errors and logging

On exception during sending, the channel captures the error, stores it on the email record and writes to the application log:

  • Look for logs with Log::error('CustomMailChannel error: ...')

Best practices & security

  • Ensure SMTP credentials stored in the database are encrypted (the seeder uses encrypt() during insert).
  • Protect access to email_senders if users can modify senders.

Useful commands

# Interactive setup and run seeder
php artisan laravel-mailer

# Run only the seeder
php artisan db:seed --class=\\Devlab\\LaravelMailer\\Database\\Seeders\\EmailSendersTableSeeder

# Run migrations (if not yet executed)
php artisan migrate

Package entry file

The service provider registers config, migrations and the command:

  • src/LaravelMailerServiceProvider.php

Support

© 2026 Devlab Studio

统计信息

  • 总下载量: 49
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固