简体   繁体   中英

change laravel 5.4 text mail content-type to text/plain

I am sending a queued text mail with laravel:

part of 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');
}

Mailable (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);
    }
}

The view empty.blade.php should only print the message:

{{ $msg }}

But neomutt receives text/html content:

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

and thunderbird also shows text/html instead of plain text:

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

What can I do to get the mail so that neomutt does not complain? When using Mail::send() I get text/plain but not with Mail::queue() .

Ok,just logically. Laravel uses SwiftMailer . Try like this (cann`t check for the moment):

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

Explanation: in callback $message has method getSwiftMessage ( see .../Illuminate/Mail/Message.php ). Via object SwiftMessage we get access to getHeaders and setContentType by SwiftMailer's specified class ( see ../swiftmailer/source-class-Swift_Mime_SimpleMimeEntity ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM