Published:March 20, 2023
Updated: November 7, 2023
Views: 4,507
Have you ever tried parsing the body of an email using traditional tools?
It can be a surprisingly tough task! Emails come in all shapes and sizes, with complex structures and various formatting elements such as headers, footers, signatures, images, and even those sneaky nested threads. Plus, every writer has their unique tone and style, which adds to the challenge of accurately identifying and extracting the information you're looking for.
But don't worry! We've got an exciting solution for you - integrating OpenAI's ChatGPT-4 with Laravel. This powerful combination will make the process of fishing out valuable nuggets from email bodies a breeze, turning it into a seamless, efficient, and enjoyable experience.
Prerequisites
PHP 8.1 or higher
Composer installed on your machine
Laravel 10
OpenAI API key
Familiarity with the basics of Laravel and ChatGPT
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
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
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_key
We will also update our config/services.php
to include the new API Key:
<?php
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'),
];
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:
<?php
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 __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.
Finally, add a new route to your routes/web.php
file to access the email parser through a POST request:
<?php
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
Done!
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!
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 by Clutch, a leading B2B ratings and reviews platform.
At Lucky Media, we offer a range of services including website development, web application development, and mobile apps development. We specialize in Statamic, React Native, Next.js, AI and ML solutions. We also provide staff augmentation and TALL stack development services.
For more insights into our work, check out our case studies on revolutionising lead generation with AI, customized coaching site, healthcare digitization, next-level performance, lead generation and patient journey, WordPress to Statamic migration, and improving user experience. These case studies provide a glimpse into how we tailor our technology choices to meet specific client needs and deliver exceptional results.
Related Posts
Stay up to date
Be updated with all news, products and tips we share!