【问题标题】:change laravel 5.4 text mail content-type to text/plain将 laravel 5.4 文本邮件内容类型更改为 text/plain
【发布时间】:2017-06-25 16:34:37
【问题描述】:

我正在使用 laravel 发送排队的文本邮件:

UploadController.php 的一部分:

public function postDelete(Request $request)
{
    $upload = Upload::where('filename',$request->filename)->where('accepted',0)->delete();

    $this->image->deleteFromUploadFolder($request->filename);

    Cache::forget('waiting_uploads');

    $msg = 'upload has been deleted';
    Mail::to('aaaaa@bbbbbb.de')->queue(new TextMail($msg));

    return redirect('upload');
}

可邮寄(TextMail.php):

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class TextMail extends Mailable
{
    use Queueable, SerializesModels;

    protected $msg;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($msg)
    {
        $this->msg = $msg;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->text('emails.empty')
                    ->subject($this->msg)
                    ->with('msg',$this->msg);
    }
}

视图empty.blade.php 应该只打印消息:

{{ $msg }}

但 neomutt 接收 text/html 内容:

[-- text/html wird nicht unterstützt ('v' benutzen, um diesen Teil anzuzeigen) --]

而且 Thunderbird 还显示 text/html 而不是纯文本:

MIME-Version: 1.0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

我该怎么做才能收到邮件,这样 neomutt 才不会抱怨? 使用Mail::send() 时,我得到text/plain,但没有Mail::queue()。

【问题讨论】:

    标签: php email laravel-5.4


    【解决方案1】:

    好的,只是逻辑上的。 Laravel 使用 SwiftMailer。试试这样(暂时无法检查):

    Mail::send('emails.empty', ['msg'=>$msg], function ($message) {
        $message->to(....)
        ->subject(....)
        ->getSwiftMessage()
        ->getHeaders()
        ->setContentType('text/plain');
    });
    

    解释:在回调中$message 有方法getSwiftMessage(见.../Illuminate/Mail/Message.php)。通过对象SwiftMessage,我们可以通过SwiftMailer's 指定类访问getHeaders 和setContentType(参见../swiftmailer/source-class-Swift_Mime_SimpleMimeEntity)。

    【讨论】:

    • 邮件已发送,但仍为text/html。
    • 当我使用Mail::send() 时,我可以拥有text/plain,但不能使用Mail::queue()(都开始一个可邮寄)
    • @haheute 好的,请显示更多代码。我确信可以“覆盖”content-type。
    猜你喜欢
    • 2014-08-18
    • 1970-01-01
    • 2014-04-16
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 2015-06-08
    • 1970-01-01
    相关资源
    最近更新 更多