omdiaries/laravel-ai-email-assistant

AI-powered email generator for Laravel (9, 10, 11 compatible) supporting OpenAI and Gemini

Maintainers

Package info

github.com/intel1590/laravel-ai-email-assistant

pkg:composer/omdiaries/laravel-ai-email-assistant

Transparency log

Statistics

Installs: 31

Dependents: 0

Suggesters: 0

Stars: 3

Open Issues: 2

v1.1.1-beta 2025-11-02 08:53 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 .txt and .html templates
  • โœ… 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:

  • welcome
  • follow_up
  • invoice
  • support

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:

  1. HTML template when HTML output is requested and a matching .html file exists.
  2. Plain text .txt template.
  3. Built-in package template.
  4. 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