laravel-ai-email-assistant maintained by omdiaries
🚀 Laravel AI Email Assistant
AI-powered email generation for Laravel 9, 10, and 11 with support for OpenAI and Google Gemini.
Generate professional, customizable emails using reusable templates and AI directly from your Laravel application.
🧠 Features
- ✅ OpenAI support
- ✅ Google Gemini support
- ✅ Common AI client interface
- ✅ Provider adapter architecture
- ✅ AI-powered email generation
- ✅ Reusable email templates
- ✅ Built-in email templates
- ✅ Custom
.txtand.htmltemplates - ✅ Customizable email tones
- ✅ Plain text and HTML output
- ✅ Laravel configuration publishing
- ✅ Placeholder replacement using
{{variable}} - ✅ Centralized Laravel logging
- ✅ Error handling
- ✅ Laravel 9, 10, and 11 support
- ✅ PHP 8.0+
📦 Installation
Install the package using Composer:
composer require omdiaries/laravel-ai-email-assistant
Publish Configuration
Publish the package configuration file:
php artisan vendor:publish --tag=ai-email-assistant-config
The configuration file will be available at:
config/aiemail.php
⚙️ Configuration
The package supports OpenAI and Google Gemini as AI providers.
Select AI Provider
Set the default provider in your .env file:
AI_PROVIDER=openai
Supported providers:
openai
gemini
OpenAI
OPENAI_API_KEY=your_openai_api_key
OPENAI_MODEL=gpt-4o-mini
Google Gemini
GEMINI_API_KEY=your_gemini_api_key
GEMINI_MODEL=gemini-1.5-flash
You only need to provide the API key for the provider you intend to use.
Email Tone
AI_EMAIL_TONE=friendly
Examples:
formal
friendly
marketing
Custom tone text is also supported.
Email Output
AI_EMAIL_OUTPUT=html
Supported formats:
plain
html
Complete .env Example
AI_PROVIDER=openai
OPENAI_API_KEY=your_openai_api_key
OPENAI_MODEL=gpt-4o-mini
GEMINI_API_KEY=your_gemini_api_key
GEMINI_MODEL=gemini-1.5-flash
AI_EMAIL_TONE=friendly
AI_EMAIL_OUTPUT=html
✉️ Generate an AI Email
Use AIEmailService to generate an email from a template.
use OmDiaries\AIEmailAssistant\Services\AIEmailService;
$service = new AIEmailService();
$email = $service->generate('welcome', [
'customer_name' => 'John',
'product' => 'My Product',
'company_name' => 'Om Diaries',
]);
The package resolves the requested template, replaces its placeholders, builds an AI prompt, and sends it to the configured provider.
📝 Email Templates
Built-in templates include:
welcomefollow_upinvoicesupport
Custom templates are loaded from:
resources/ai-templates/
Plain Text Template
Create resources/ai-templates/welcome.txt:
Subject: Welcome to {{product}}
Hello {{customer_name}},
Welcome to {{product}}! We're excited to have you.
Best regards,
{{company_name}}
HTML Template
Create resources/ai-templates/welcome.html.
When HTML output is requested, the HTML template is preferred. If it is unavailable, the package falls back to the .txt template.
Template Resolution
Templates are resolved in this order:
- HTML template when HTML output is requested and a matching
.htmlfile exists. - Plain text
.txttemplate. - Built-in package template.
- An exception is thrown if the requested template does not exist.
🔤 Template Placeholders
Templates can use placeholders such as:
{{customer_name}}
{{product}}
{{company_name}}
Pass values when generating the email:
$email = $service->generate('welcome', [
'customer_name' => 'John',
'product' => 'My Product',
'company_name' => 'Om Diaries',
]);
Dynamic values are escaped when HTML output is enabled.
🎨 Email Tones
Supported examples:
formal
friendly
marketing
Custom tones are also supported, for example:
AI_EMAIL_TONE=professional and concise
📄 Output Formats
Plain Text
AI_EMAIL_OUTPUT=plain
HTML
AI_EMAIL_OUTPUT=html
The configured AI provider will be instructed to generate the requested output format.
🤖 Supported AI Providers
OpenAI
Adapter:
src/Adapters/OpenAIAdapter.php
Configuration:
AI_PROVIDER=openai
OPENAI_API_KEY=your_openai_api_key
OPENAI_MODEL=gpt-4o-mini
Google Gemini
Adapter:
src/Adapters/GeminiAdapter.php
Configuration:
AI_PROVIDER=gemini
GEMINI_API_KEY=your_gemini_api_key
GEMINI_MODEL=gemini-1.5-flash
Both providers implement the same AIClientInterface.
🧩 AI Provider Architecture
Common contract:
OmDiaries\AIEmailAssistant\Contracts\AIClientInterface
Generic AI text generation:
public function generate(
string $prompt,
array $options = []
): string;
Email generation:
public function generateEmail(
string $prompt,
string $tone = 'friendly',
string $output = 'plain'
): string;
Current structure:
src/
├── Adapters/
│ ├── OpenAIAdapter.php
│ └── GeminiAdapter.php
├── Contracts/
│ └── AIClientInterface.php
├── Services/
│ └── AIEmailService.php
└── Support/
└── PromptTemplates.php
🔧 Generic AI Text Generation
Adapters support generic text generation through the common interface:
$client->generate(
'Summarize this customer message',
[
'temperature' => 0.2,
]
);
Provider-specific options can be passed through the $options array.
🔧 Adding a New AI Provider
Implement:
OmDiaries\AIEmailAssistant\Contracts\AIClientInterface
Create an adapter:
src/
└── Adapters/
└── YourAIProviderAdapter.php
Implement both interface methods:
public function generate(
string $prompt,
array $options = []
): string;
public function generateEmail(
string $prompt,
string $tone = 'friendly',
string $output = 'plain'
): string;
Then register the provider through the package configuration/service architecture.
🔐 Security
Never commit API keys to your repository.
Use your application's .env file:
OPENAI_API_KEY=your_openai_api_key
GEMINI_API_KEY=your_gemini_api_key
Make sure .env is excluded from version control.
🧪 Testing
Run the PHPUnit test suite with:
vendor/bin/phpunit
📁 Project Structure
src/
├── Adapters/
│ ├── OpenAIAdapter.php
│ └── GeminiAdapter.php
├── Contracts/
│ └── AIClientInterface.php
├── Services/
│ └── AIEmailService.php
└── Support/
└── PromptTemplates.php
config/
└── aiemail.php
resources/
└── ai-templates/
📋 Requirements
- PHP
>= 8.0 - Laravel 9
- Laravel 10
- Laravel 11
- Composer
- API key for the selected AI provider
🤝 Contributing
Pull requests are welcome.
For new AI providers, bug fixes, improvements, or major features, please open an issue first to discuss the proposed implementation.
🪪 License
This package is open-sourced software licensed under the MIT License.
© 2026 OmDiaries