I want to create an *.eml file, which I want to use as a template for Outlook. My project is using Laravel.
I’m aware of the SwiftMailer function toString(), which is used to convert the message into MIME-format. See below for my approach. That code generates the header properly, but body and and html-template(blade), which I pass to the method, won’t be displayed the way I want to...
The Mail mustn't be send, but generated.
I didn’t find any related problems on the Internet. Saving generated *.eml files doesn’t seem to be popular :) Does anyone can think of a solution?
public function generateEmlFile(Request $request) {
//...
$data = array('name' => "recipient");
Mail::send('emails.ordering', $data, function($message) {
$message->from('[email protected]');
$message->to('[email protected]');
$message->subject('Subject');
$content = $message->toString();
dd($content);
});
return true;
}
a dump of the work so far:
Message-ID: <4900a58cfb9b22900d8500b6e80f3022@examlpe-domain>\r\n
Date: Wed, 17 Jul 2019 15:51:38 0200\r\n
Subject: Subject\r\n
From: [email protected]\r\n
Reply-To: example-project <[email protected]>\r\n
To: [email protected]\r\n
MIME-Version: 1.0\r\n
Content-Type: text/plain; charset=utf-8\r\n
Content-Transfer-Encoding: quoted-printable\r\n
CodePudding user response:
I recommend not to use any external Swiftmailer transport extension or do any custom coding in EventServiceProvider. The following solution is much simpler:
Create logdir logs/emails
Add an extra channel configuration to config/logging.php
:
'channels' => [
// ...
'emails' => [
'driver' => 'single',
'path' => storage_path('logs/emails/' . \Illuminate\Support\Str::uuid() . '.eml'),
'level' => 'debug',
],
And then simply use the following in your .env
:
MAIL_MAILER=log
MAIL_LOG_CHANNEL=emails
That way, you get wonderful logs/emails/<UUID>.eml
files with only one drawback (that does no harm): The first line (before Message-ID: ...
) of the email header is prefixed with log formatting prefix [_TIMESTAMP_] local.DEBUG:
. As a non-standard email header is ignored by all/most email clients, this should be no issue in development.
CodePudding user response:
you can listen to the LogSentMessage event
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 723a290..f7568ec 100644
--- a/app/Providers/EventServiceProvider.php
b/app/Providers/EventServiceProvider.php
@@ -18,6 18,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
'Illuminate\Mail\Events\MessageSent' => [
'App\Listeners\LogSentMessage',
],
];
after adding these 3 lines run php artisan event:generate
this should create the file app/Listeners/LogSentMessage.php
inside the handle function you can access the complete msg -> $event->message->toString();
https://laravel.com/docs/6.x/mail#events
CodePudding user response:
If you are not sending the email in the application, there's no need to use the laravel Mail
facade.
There is a extension for Swiftmailer that supports saving emails as .eml
files
https://github.com/geekdevs/swift-mailer-extensions