How to offer inbox to your customer in php using Sendgrid
To set up email parsing with SendGrid, follow these steps:
- Create a free account on sendgrid.com.
- Once logged in, navigate to https://app.sendgrid.com/settings/parse and configure your domain.
- Ensure that you create an MX record in your DNS with the following details:
- Hostname: mailincoming.yourdomain.com
- Priority: 10
- Destination: mx.sendgrid.net
- Configure mailincoming.yourdomain.com inside SendGrid and enter the webhook URL.
- With these steps completed, you can now send emails to any address, such as aaa@mailingcoming.yourdomain.com, and receive the webhook content without any issues.
Be sure the check the box labeled “Post the raw, full MIME message.” when setting up SendGrid.
Reference : https://docs.sendgrid.com/for-developers/parsing-email/setting-up-the-inbound-parse-webhook
You can use the following package may be
- Laravel inbox
Let me explain how to setup sendgrid with laravel inbox package.
Install the package then change the setup as follow
return [
'driver' => env('MAILBOX_DRIVER', 'sendgrid'),
'model' => \BeyondCode\Mailbox\InboundEmail::class,
'path' => 'laravel-mailbox',
'store_incoming_emails_for_days' => 7,
'only_store_matching_emails' => false,
'basic_auth' => [
'username' => env('MAILBOX_HTTP_USERNAME', 'laravel-mailbox'),
'password' => env('MAILBOX_HTTP_PASSWORD','123456'),
],
'services' => [
'sendgrid' => [
'key' => env('MAILBOX_SENDGRID_KEY'),
],
],
];
Your typical AppServiceProvider should be like this
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use BeyondCode\Mailbox\InboundEmail;
use BeyondCode\Mailbox\Facades\Mailbox;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Mailbox::from('{username}@mailincoming.yourdomain.com', function (InboundEmail $email) {
echo $email->subject();
echo $email->html();
echo "Jell";
foreach ($email->attachments() as $attachment) {
$filename = $attachment->getFilename();
$attachment->saveContent(storage_path($filename));
}
});
}
}
Make sure you read the setup wehbook url correctly read the documentation.
the MAILBOX_HTTP_PASSWORD environment variable. The default username is "laravel-mailbox", but you can change it using the MAILBOX_HTTP_USERNAME environment variable. :::
You can then set your MAILBOX_DRIVER to "sendgrid".
Next you will need to configure SendGrid Inbound parse, to send incoming emails to your application at /laravel-mailbox/sendgrid. Use the username and the password that you generated for the URL.
If your application is at https://awesome-laravel.com, it would be https://laravel-mailbox:YOUR-GENERATED-PASSWORD@awesome-laravel.com/laravel-mailbox/sendgrid.
Reference : https://beyondco.de/docs/laravel-mailbox/drivers/drivers
But if you don’t like it or confusing laravel inbox url then you can build something like this
namespace App\Http\Controllers;
use BeyondCode\Mailbox\Drivers\SendGrid;
use Illuminate\Http\Request;
use SendGrid\Client;
class InboxController extends Controller
{
public function getInbox()
{
$content = request()->getContent();
$content1 =json_decode($content);
$email = new \SendGrid\Mail\Mail();
$email->setFrom($_POST['from_email']);
$email->setSubject($_POST['subject']);
$email->addTo($_POST['to_email']);
$email->addContent("text/plain", $_POST['text']);
$from = $email->getFrom();
$subject = $email->getSubject();
echo $from;
}
}