Introduction
In project management or support management tools, you will see this a lot: you can reply to an email message and it is automatically visible in a web application. Somehow, these tools were able to get those email messages right into their system.
In this article, we are going to have a look at how we can pipe emails to our Laravel 4 application. For that, we start with a fresh Laravel 4 project, installed through Composer as seen here.
composer create-project laravel/laravel your-project-name --prefer-dist
Creating an Artisan command
To be able to get our email into our application, we somehow have to pipe the email through the command line into our application. Luckily, Laravel has a command line tool called Artisan, which is able to perform multiple tasks. To see a list of all tasks that Artisan can run, you can run php artisan list in the root of your project.
In this case, we want it to perform a very specific task: accept a raw email and use it within our application. Unfortunately, this is not one of the basic functions Artisan can handle. We can easily extend it with a new command: php artisan email:parse. We’ll then just start up Artisan and perform a certain task, which, in this case, is called email:parse.
Our first step is to create this command. You can create new commands through Artisan’s own command for creating new commands. Just run the following in the root of your project:
php artisan command:make EmailParserCommand
If everything went smoothly, you will now find a file called EmailParserCommand.php in the directory app/commands. Open it up in your favorite editor and have a look at the $name and $description properties. We can customize this to whatever we want. By giving it a clear name and description, the command will be listed nicely in the list of artisan commands.
I changed it to this, for example:
/**
* The console command name.
*
* @var string
*/
protected $name = 'email:parse';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Parses an incoming email.';
Register the command
When we run php artisan email:parse in the root of our project, you will receive a message that this command is not yet registered. Our next step is to make sure this command is registered within Artisan. Let’s open up the file app/start/artisan.php and add Artisan::add(new EmailParserCommand); to the end of the file to register our newly created command. We can now actually run the list command again to see our email:parse command listed. Notice that your just filled in name and description is displayed here.
Retrieve the raw email
Whenever the command is called through Artisan, it will always call the fire method. So initially, we have to add our parsing of the email in here. The email is currently sitting in our IO stream which we can retrieve from php://stdin. We open up this IO stream and then collect the email in small parts, until we have read the whole stream.
/**
* Execute the console command.
*
* @return void
*/
public function
Truncated by Planet PHP, read more at the original (another 18279 bytes)
more
{ 0 comments... » Piping Emails to a Laravel Application read them below or add one }
Post a Comment