DEV Community

Cover image for Effortless Email Archiving: Saving Sent Emails on Server with Laravel IMAP
Koussay
Koussay

Posted on • Updated on

Effortless Email Archiving: Saving Sent Emails on Server with Laravel IMAP

Hello,
Sending emails with Laravel is really one of the easiest things to do. For example, sending password resets, or account activation, but sometimes, you want to send emails to your users and leave a trail for that. In order to do so, you have 2 options, either storing the email in the database, or save it on the IMAP server.
In this tutorial we will see the second option, append the sent email to the IMAP server. So let's get started.

NOTE: : This tutorial will assume you have a Laravel installation in place already.

The first thing to do is to install laravel-imap package

composer require webklex/laravel-imap
Enter fullscreen mode Exit fullscreen mode

After installing it, we will set up the IMAP credentials in our .env file.
After opening the .env file, paste the following code at the end of the file.

IMAP_HOST="imaphost"
IMAP_PORT="993"
IMAP_ENCRYPTION="ssl"
IMAP_VALIDATE_CERT="true"
IMAP_USERNAME="email"
IMAP_PASSWORD="password"
IMAP_DEFAULT_ACCOUNT=default
IMAP_PROTOCOL=imap
Enter fullscreen mode Exit fullscreen mode

Then change the values in according to your configuration.

The next step is to generate a Mailable so we can send it, to do so, just run this command:

php artisan make:mail TestMail
Enter fullscreen mode Exit fullscreen mode

This command will generate a new mailable class called TestMail.php.
All you need to do now is open the file and edit it (mainly adjust the view name).

NOTE: : You can see other tutorials about sending emails using Laravel.
Now after everything is in place, now we will try to send the email and save it on the IMAP server.

So to send the email, all you need to do is the following

$mail = Mail::to('hello@gmail.com')->send(new TestMail());
Enter fullscreen mode Exit fullscreen mode

Note here, we saved the result in the $mail variable, because we will use that to store the sent email.
After the email was sent, we need to connect to our IMAP server now.

$client = Client::account('default');
$client->connect();
$folder = $client->getFolderByName('Sent');
Enter fullscreen mode Exit fullscreen mode

The code above will try to connect to the IMAP server using the credentials we put earlier in the .env file. After the connection has been made, we will retrieve the Sent folder.

NOTE: : The sent folder name is usually Sent, this depends on your IMAP configuration.

After successfully getting the folder, all we need now is to append it, by using the appendMessage method.

$result = $folder->appendMessage($mail->getSymfonySentMessage()->toString(), ['\Seen'], now()->format("d-M-Y h:i:s O"));
Enter fullscreen mode Exit fullscreen mode

Now let's break down this method, and see:
The first parameter, is the message in a string format.
The $mail variable will be a SymfonySentMessage type, so it has a method called getSymfonySentMessage()->toString(), this method will return the string format of the message.
This is an example of the result:

From: Laravel <hello@example.com>
To: hello@gmail.com
Subject: Test Mail
Message-ID: <1259c22f78de504c28beabdb9eda5636@example.com>
MIME-Version: 1.0
Date: Sun, 11 Jun 2023 13:37:36 +0000
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

HELLO WORLD
Enter fullscreen mode Exit fullscreen mode

Using this string, we will be able to append the sent message,

The second parameter is the flag, in this example, we will use the seen flag, so it will be seen on the IMAP. You can learn more about flags on this RFC 2060

And finally, the date, we will use that to indicate when this message will be saved at IMAP.

Finally, we will check the result of the appendMessage method.
The result will be something like that

'OK [APPENDUID 1655722943 20] Append completed (0.018 + 0.135 + 0.016 secs)';
Enter fullscreen mode Exit fullscreen mode

If you have this string, then everything went okay.

I hope you liked this tutorial, and you gain information about it.
If something is not clear or you want ask about further information, you can contact me on Twitter or comment down below.
Original Post
Thank you again and have a nice day.

Top comments (0)