将laravel 5.4文本邮件内容类型更改为text / plain

时间:2017-06-24 22:04:12

标签: php email laravel-5.4

我正在发送带有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');
}

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

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

{{ $msg }}

但neomutt接收文字/ 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()

1 个答案:

答案 0 :(得分: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,我们可以通过getHeaders指定的类访问setContentTypeSwiftMailer's。(请参阅../swiftmailer/source-class-Swift_Mime_SimpleMimeEntity)。