如何使用Swift Mailer生成* .eml文件(在Laravel中) [英] How to generate *.eml file with Swift Mailer (in Laravel)

查看:67
本文介绍了如何使用Swift Mailer生成* .eml文件(在Laravel中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个* .eml文件,我希望将其用作Outlook的模板.我的项目正在使用Laravel.

I want to create an *.eml file, which I want to use as a template for Outlook. My project is using Laravel.

我知道SwiftMailer函数toString(),该函数用于将邮件转换为MIME格式.请参阅下文了解我的方法.该代码可以正确生成标头,但是我传递给该方法的body和html-template(blade)不会以我想要的方式显示...

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.

我在互联网上没有发现任何相关问题.保存生成的* .eml文件似乎并不流行:)有谁能想到解决方案?

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('example@mail.com');
        $message->to('example@mail.com');
        $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: example@mail.com\r\n
Reply-To: example-project <example-project@examlpe-domain.com>\r\n
To: example@mail.com\r\n
MIME-Version: 1.0\r\n
Content-Type: text/plain; charset=utf-8\r\n
Content-Transfer-Encoding: quoted-printable\r\n

推荐答案

我建议不要使用任何外部Swiftmailer传输扩展或在EventServiceProvider中进行任何自定义编码.以下解决方案要简单得多:

I recommend not to use any external Swiftmailer transport extension or do any custom coding in EventServiceProvider. The following solution is much simpler:

创建日志目录日志/电子邮件

config/logging.php 中添加额外的频道配置:

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',
        ],

然后只需在 .env 中使用以下内容:

And then simply use the following in your .env:

MAIL_MAILER=log
MAIL_LOG_CHANNEL=emails

这样,您将获得精美的 logs/emails/< UUID> .eml 文件,但只有一个缺点(无害):第一行(在 Message-ID之前:... )的开头加上日志格式前缀 [_ TIMESTAMP_] local.DEBUG:.由于所有/大多数电子邮件客户端都会忽略非标准的电子邮件标头,因此在开发中应该没有问题.

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.

这篇关于如何使用Swift Mailer生成* .eml文件(在Laravel中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆