Building an Email Parser with ChatGPT-4 and Laravel

March 20, 2023 · 7 min read

Have you ever tried parsing the body of an email using traditional tools?
Parsing the body of an email is not easy at all. Traditional methods often fall short when faced with the different and complex parts of email content. Complex structures, lots of different formatting elements like headers, footers, signatures, images, and those intricate nested threads, all combine to create a difficult task. To complicate matters further, each individual’s unique writing style and tone can hide the important information buried within an email’s text.
But there’s good news for developers and businesses who struggle with this. The new AI language model, ChatGPT-4, from OpenAI, can now work with Laravel. This new combination can change the way you work with emails. Let’s look into how this powerful pair can make things better for you.
The Power of AI
Integrating ChatGPT-4 with Laravel brings the advanced abilities of artificial intelligence into the reliable and developer-friendly environment of Laravel. This mix opens up new chances for managing email content with great ease and precision.
Intelligent Content Extraction ChatGPT-4’s advanced natural language processing algorithms can break down 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.
Adaptation to Writing Styles The AI can match different writing styles, making sure it captures the message’s core, no matter the tone. ChatGPT-4 works well with both formal business emails and casual chats, handling language nuances to get the info you need.
Automated Sorting and Categorization Laravel’s framework, combined with the AI’s learning capabilities, can can sort emails automatically. This means that emails can be sorted into predefined buckets such as ’urgent’, ’follow-up’, or ’informational’, simplifying the management process and saving valuable time.
Enhanced User Experience Developers using Laravel will find that integrating ChatGPT-4 improves the user experience by simplifying complex back-end processes. This leads to a smoother and more user-friendly interface for managing emails.
Scalability and Flexibility As your business grows, your email parsing system can too. Laravel’s scalability and ChatGPT-4’s flexibility mean that your email handling can get better and adapt with your business.
Prerequisites
PHP 8.1 or higher
Composer installed on your machine
Laravel 10
OpenAI API key
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-parserAfter the installation is complete, cd into the email-parser directory:
cd email-parserStep 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/guzzleNext, let’s install the OpenAI PHP client to make interacting with the ChatGPT-4 API more convenient:
composer require openai-php/clientStep 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:
OPENAI_KEY=your_openai_api_keyWe 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', 'api.mailgun.net'),
'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 --invokableNow 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.
EOT;
$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
__invokemethod is called first we validate the requestPrompt generation - A multiline "here-doc" string is assigned to the
$promptvariable. 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 messageResponse 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 might need some cleaning to make sure the parser correctly pulls out the important parts. This could involve removing HTML tags or unwanted parts before giving the text 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.
Conclusion
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.
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 leading Laravel Development Agency
Technologies

Stay up-to-date
Be updated with all news, products and tips we share!
On this page
- The Power of AI
- Prerequisites
- Step 1: Setting up your Laravel project
- Step 2: Installing Guzzle and OpenAI PHP Client
- Step 3: Setting up the ChatGPT-4 API client
- Step 4: Creating the email parser controller
- Step 6: Adding a route to access the email parser
- Real-World Examples
- Conclusion
- Bring Your Ideas to Life 🚀

