Building an Email Parser with ChatGPT-4 and Laravel

lokman musliu
Lokman Musliu

March 20, 2023 · 7 min read · 5,298 views

Laravel and OpenAI Blogpost Image

Have you ever tried parsing the body of an email using traditional tools?

Parsing the body of an email is not for the faint of heart. Traditional methods often fall short when faced with the diverse and intricate nature of email content. Complex structures, a multitude of formatting elements like headers, footers, signatures, images, and those intricate nested threads, all combine to create a formidable task. To complicate matters further, each individual's unique writing style and tone can obscure the essential information buried within an email's text.

However, there's good news for developers and businesses alike who grapple with this challenge. The integration of OpenAI's latest AI language model, ChatGPT-4, with the robust PHP framework Laravel, offers a transformative approach to email parsing. Let's delve into how this dynamic duo can revolutionize your workflow.

The Power of AI

Integrating ChatGPT-4 with Laravel brings the cutting-edge capabilities of artificial intelligence into the dependable and developer-friendly environment of Laravel. This combination unlocks new possibilities for handling email content with unprecedented ease and accuracy.

  1. Intelligent Content Extraction ChatGPT-4's advanced natural language processing algorithms can dissect complex email structures to extract relevant information. From identifying key points in a lengthy thread to filtering out unnecessary details, the AI model is trained to understand context and content with remarkable precision.

  2. Adaptation to Writing Styles The AI is adept at adjusting to various writing styles, ensuring that the essence of the message is captured no matter how it's conveyed. Whether it's a formal business inquiry or an informal chat, ChatGPT-4 can navigate the nuances of language to retrieve the information you need.

  3. Automated Sorting and Categorization Laravel's framework, combined with the AI's learning capabilities, can automate the categorization of emails. This means that emails can be sorted into predefined buckets such as 'urgent', 'follow-up', or 'informational', streamlining the management process and saving valuable time.

  4. Enhanced User Experience Developers using Laravel will find that integrating ChatGPT-4 enhances the user experience by simplifying complex back-end processes. The result is a smoother, more intuitive interface for end-users, making email management a far less daunting task.

  5. Scalability and Flexibility As your needs grow and evolve, so too can your email parsing solution. The scalability of Laravel and the versatility of ChatGPT-4 ensure that your email parsing capabilities can expand and adapt alongside your business.


  1. PHP 8.1 or higher

  2. Composer installed on your machine

  3. Laravel 10

  4. OpenAI API key

  5. Familiarity with the basics of Laravel and ChatGPT

Step 1: Setting up your Laravel project

First, open your terminal and navigate to your desired directory. Run the following command to install a new Laravel project:

composer create-project --prefer-dist laravel/laravel email-parser

After the installation is complete, cd into the email-parser directory:

cd email-parser

Step 2: Installing Guzzle and OpenAI PHP Client

In order to use the Laravel HTTP Client we first need to install Guzzle with the following command:

composer require guzzlehttp/guzzle

Next, let's install the OpenAI PHP client to make interacting with the ChatGPT-4 API more convenient:

composer require openai-php/client

Step 3: Setting up the ChatGPT-4 API client

To store the API key and other configuration details, add the following lines to the .env file in the root directory of your project:


We will also update our config/services.php to include the new API Key:


return [

    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
        'endpoint' => env('MAILGUN_ENDPOINT', ''),
        'scheme' => 'https',

    'postmark' => [
        'token' => env('POSTMARK_TOKEN'),

    'ses' => [
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
    // Here is our OpenAI key:
    'open_ai' => env('OPEN_AI_KEY'),


Step 4: Creating the email parser controller

Let's create the main controller: EmailParserController. To do this, run the following command:

php artisan make:controller EmailParserController --invokable

Now with our controller in place here is the code that will take an email HTML and extract only the body of the email:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use OpenAI;

class EmailParserController extends Controller
     * Handle the incoming request.
    public function __invoke(Request $request)
       $validated = $request->validate([
            'email' => 'required',

        $prompt = <<<'EOT'
        You are an email parsing expert.
        Parse the body of the email without the replies, filler text, or any other signatures.
        Don\'t add any text in the beginning or in the end, your reply should only consist of the extract text.
        Do not modify the original email reply in any way, don't add or remove any text.

        $client = OpenAI::client(config('services.open_ai'));

        $response = $client->chat()->create([
            'model' => 'gpt-4',
            'messages' => [
                    'role' => 'system',
                    'content' => Str::squish($prompt),
                    'role' => 'user',
                    'content' => sprintf('Parse the body of this email without the replies, filler text, or any other signatures: %s', $validated['email']),

        return $response->choices[0]->message->content;

Okay now let's break down what's happening in this controller:

  • Request validation - When the __invoke method is called first we validate the request

  • Prompt generation - A multiline "here-doc" string is assigned to the $prompt variable. This prompt contains instructions for parsing the body of an email, specifically to extract the text without replies, filler text, or any other signatures. It also specifies that the extracted text should remain unmodified.

  • Open AI client - An OpenAI client is created using the provided configuration settings (API key and endpoint) from the config('services.open_ai').

  • API request to ChatGPT - An API request is made to ChatGPT via OpenAI's chat() method. The model parameter specifies which model to use (in this case, GPT-4). The messages parameter accepts an array of messages with specific roles (system and user) and their associated content.

  • System role - The system role sets up the context by passing the squished prompt. Squishing the prompt removes unnecessary whitespace and newline characters without altering its actual content.

  • User role - The user role message takes the email content from the initial request ($request->input('email')) and asks GPT-4 to parse it based on the instructions provided by the system role message

  • Response Choices - The ChatGPT API responds with a list of choices. In this implementation, only the content from the first choice (message) is returned, which contains the relevant extracted text as per the given instructions.

Step 6: Adding a route to access the email parser

Finally, add a new route to your routes/web.php file to access the email parser through a POST request:


use App\Http\Controllers\EmailParserController;
use Illuminate\Support\Facades\Route;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!

Route::post('/parse', EmailParserController::class);
Please note that since the parser is designed to process raw email text (including HTML), the text from the provided HTML emails may require some clean-up to ensure the parser accurately extracts the relevant portions. This might include eliminating HTML tags or unnecessary elements before passing the text content to the parser. Consider using a package like Html Purifier


Real-World Examples

According to ChatGPT here are some possible use cases for email parsing:

  • Customer support: Automatically categorize, prioritize, and route incoming customer emails to the appropriate support agents based on keywords or phrases, ensuring timely and efficient responses.

  • Lead generation: Extract contact information from emails, such as names, phone numbers, or email addresses to grow your database and improve targeting for future campaigns or outreach.

  • Event registration: Parse event registrations or RSVP emails to automatically populate attendee lists and send personalized confirmation messages.

  • Data extraction: Extract key data from specific fields or attachments, like invoices, purchase orders, or contracts, enabling efficient organization, analysis, or integration into your workflow.

  • Newsletter subscriptions: Automate the process of adding new subscribers to your mailing list or database upon receiving subscription confirmation emails, ensuring no subscriber is missed.

  • Sentiment analysis: Parse email content to evaluate customer sentiment for product feedback, customer satisfaction, or trend analysis.

  • Email summarization: Aggregate and summarize the content of important emails, providing a condensed overview of relevant information for quick consumption.

  • Centralized database updates: Auto-update CRM, ERP, or other systems with extracted information from emails such as transactions, project details, or status updates.

  • Calendar management: Extract information from meeting requests, such as dates, times, and locations, to automatically update your digital calendar or send reminders.

  • Automatic data-syncing: Sync email data across multiple platforms, such as spreadsheets, project management tools, or team communication channels, to ensure data consistency and stay organized.


That's it! Now you have a fully functional email parser utilizing the power of ChatGPT-4 and Laravel. You can test it out by sending an email text through a POST request to /parse.

As always, don't forget to optimize and enhance the code to fit your specific needs.

Happy parsing!

Bring Your Ideas to Life 🚀

If you need help with a Laravel project let's get in touch.

Lucky Media is proud to be recognized as a Top Laravel Development Agency

lokman musliu
Lokman Musliu

Founder and CEO of Lucky Media


Heading Pattern

Related Posts

Stay up to date

Be updated with all news, products and tips we share!