omdiaries / laravel-ai-email-assistant
AI-powered email generator for Laravel (9, 10, 11 compatible) supporting OpenAI and Gemini
Package info
github.com/intel1590/laravel-ai-email-assistant
pkg:composer/omdiaries/laravel-ai-email-assistant
Requires
- php: >=8.0
- guzzlehttp/guzzle: ^7.0
- illuminate/support: ^9.0|^10.0|^11.0
- openai-php/client: ^0.9 || ^0.10
This package is auto-updated.
Last update: 2026-08-23 15:48:22 UTC
README
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