<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Exorus</title>
    <description>The latest articles on DEV Community by Exorus (@exorus).</description>
    <link>https://dev.to/exorus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1975801%2F37230c9c-7094-4541-aef3-aa1d71e29f0d.png</url>
      <title>DEV Community: Exorus</title>
      <link>https://dev.to/exorus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/exorus"/>
    <language>en</language>
    <item>
      <title>How to integrate MailCare with your Laravel 11 application</title>
      <dc:creator>Exorus</dc:creator>
      <pubDate>Wed, 23 Oct 2024 13:01:26 +0000</pubDate>
      <link>https://dev.to/exorus/how-to-integrate-mailcare-with-your-laravel-11-application-563c</link>
      <guid>https://dev.to/exorus/how-to-integrate-mailcare-with-your-laravel-11-application-563c</guid>
      <description>&lt;p&gt;In this tutorial, we’ll walk through how to integrate &lt;a href="https://mailcare.io" rel="noopener noreferrer"&gt;MailCare&lt;/a&gt; with a Laravel application to handle incoming emails via webhooks. This approach is useful for automating tasks such as managing customer support requests, processing feedback, or any other email-based automation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A basic Laravel application set up. (If not, follow the instructions on the &lt;a href="https://laravel.com/docs/11.x/installation" rel="noopener noreferrer"&gt;Laravel documentation&lt;/a&gt;.)&lt;/li&gt;
&lt;li&gt;An account on &lt;a href="https://mailcare.io" rel="noopener noreferrer"&gt;MailCare&lt;/a&gt; for setting up email reception and webhook automation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;MailCare will provide you with a temporary domain where you can receive up to 100 emails per month for free.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Case
&lt;/h2&gt;

&lt;p&gt;Imagine you're building an automated support system where users email you for assistance. Instead of manually processing each email, you'd want to capture and handle them directly within your Laravel app. MailCare provides a temporary domain for receiving emails, and it can forward email metadata as JSON to your Laravel app using webhooks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Set Up MailCare
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;a href="https://mailcare.io" rel="noopener noreferrer"&gt;MailCare.io&lt;/a&gt; and sign up or log in to your account.&lt;/li&gt;
&lt;li&gt;MailCare will provide you with a temporary domain (e.g., &lt;code&gt;v9w3fyarsq.mailcare.dev&lt;/code&gt;). You can customize it if needed.&lt;/li&gt;
&lt;li&gt;Navigate to the &lt;strong&gt;Automations&lt;/strong&gt; section, then click on &lt;strong&gt;Add Automation&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the automation form:

&lt;ul&gt;
&lt;li&gt;Give it a name like &lt;strong&gt;Catch All&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In the field &lt;strong&gt;Webhook URL to Call&lt;/strong&gt;, enter the URL of your Laravel app where you’ll handle the webhook. For example: &lt;code&gt;https://mylaravelapp.com/api/incoming-emails&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Payload Format&lt;/strong&gt; field is already set to &lt;strong&gt;JSON Metadata Email&lt;/strong&gt;, which is the default option, so no changes are needed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Save the automation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuewyfqm5xss0keuw2f1y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuewyfqm5xss0keuw2f1y.png" alt="Image description" width="800" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s it for the MailCare setup! Now let's move on to the Laravel side.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Set Up Laravel to Handle Incoming Emails
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2.1 Define the Route
&lt;/h3&gt;

&lt;p&gt;To keep things simple, we’ll define a POST route in &lt;code&gt;routes/api.php&lt;/code&gt; that will handle the incoming webhooks directly without creating a separate controller.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;routes/api.php&lt;/code&gt; file, add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\Route&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Http\Request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\Log&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/incoming-emails'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Extract email data from the JSON payload&lt;/span&gt;
    &lt;span class="nv"&gt;$emailData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'data'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Log specific details of the email for better clarity&lt;/span&gt;
    &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Email received from:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sender'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$emailData&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'sender'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;'Unknown'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Email received to:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'inbox'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$emailData&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'inbox'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;'Unknown'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Email subject:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'subject'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$emailData&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'subject'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;'No Subject'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="c1"&gt;// Extract email data from the JSON payload&lt;/span&gt;
    &lt;span class="nv"&gt;$emailData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Log the email data for debugging and verification&lt;/span&gt;
    &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Received email webhook from MailCare:'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$emailData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// You can also handle the email data here (e.g., store in database, trigger actions, etc.)&lt;/span&gt;

    &lt;span class="c1"&gt;// Here, you could add further processing, such as:&lt;/span&gt;
    &lt;span class="c1"&gt;// - Storing the email in your database&lt;/span&gt;
    &lt;span class="c1"&gt;// - Triggering a notification or action based on the email content&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;noContent&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What Else Can You Do?
&lt;/h2&gt;

&lt;p&gt;There are many ways to extend this integration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Filters in Automations&lt;/strong&gt;: Only forward specific emails by setting up filters in MailCare, such as sender or subject-based rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security with a Secret Token&lt;/strong&gt;: Protect your webhook by adding a secret token and verifying it in Laravel to ensure the request is coming from MailCare.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retrieve Email Body&lt;/strong&gt;: Modify the webhook format to include the email body in HTML or plain text, allowing you to process the content directly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handle Attachments&lt;/strong&gt;: Use MailCare’s API to retrieve attachments and process them in your Laravel app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parse Links and Extract Data&lt;/strong&gt;: Extract useful information from email bodies, such as links or specific keywords, to automate further actions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more details, check out the &lt;a href="https://mailcare.io/docs/webhooks" rel="noopener noreferrer"&gt;MailCare Webhooks documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we successfully integrated &lt;a href="https://mailcare.io" rel="noopener noreferrer"&gt;MailCare&lt;/a&gt; with a Laravel 11 application to handle incoming emails via webhooks. We explored how to log email details and set the foundation for more advanced use cases. From filtering emails to handling attachments or parsing content, the possibilities are extensive.&lt;/p&gt;

&lt;p&gt;With MailCare and Laravel, you can build powerful email-driven workflows tailored to your needs. Now that you have the basics set up, it's time to expand and customize it for your specific use case!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
