简体   繁体   中英

Laravel-Mail: How to pass ->attachData to view

I'm trying to use an inline attachment in laravel mail but seems unfortunate. I also tried this one but doesn't work by embeding of raw data. I have a base 64 image/png here is an example of it.

Now I'm trying to use attachData but how can I pass the ->attachData to my mailtransaction.blade . I supposedly get the attachData from my controller but what variable should I call?

Controller.php

Mail::send(['html'=>'mailtransaction'], $data_content, function($msg) use ($to, $issueType, $base64){
        $msg->to($to); // change this upon finishing
        $msg->attachData($base64, 'test.png', ['mime'=>'image/png']);
        $msg->subject($issueType);
      });

mailtransaction.blade

<!DOCTYPE html>
<html>
<head>
</head>
<body style="width:100%;">
    <div style="border:0px solid #000; width:1000px !important;">
        <div style="display: inline-block;">
            <img src="{{$message->embed('storage/app/public/images/logo.png')}}" height="50px" width="50px">
        </div>
        <div style="display: inline-block; vertical-align: top;">
            <div style="font-size:24px; margin-bottom: -10px;">Fraud Detection Tool</div>
            <div>Suspicious Transaction details</div>
        </div>
        <hr style="border:0px; border-bottom:1px solid #000; width:1000px;">
        <div class="container">
            {{$msg}}
            //I supposedly get the attachData from my controller but what variable should I call?
        </div>
        <hr style="width:1000px;">
        <div class="container_mail" style="width:600px !important;">
            <img src="{{}}" height="auto" style="max-width: 1000px">
        </div> 
    </div>
</body>
</html>

You can pass data to your view as the second argument of send() method and it needs to be an array of data. Change your controller to -

Mail::send('mailtransaction', ['data_content'=>$data_content,'base64'=>$base64], function($msg) use ($to, $issueType){
    $msg->to($to); // change this upon finishing
    $msg->attachData($request->getBase64, 'test.png', ['mime'=>'image/png']);
    $msg->subject($issueType);
  });

And in your view, you can access data_content and base64 as-

{{$data_content}}
{{$base64}}

The attachData method adds an email attachment and doesn't work for inline images. You need to add your image data to your $data_content variable and refer to it from there:

$data_content['attachedImage'] = ...; // Get the regular data of the image, not the base64 version

and then in your template use the embedData method:

<img src="{{ $message->embedData($attachedImage, 'test.png') }}" align="right" width="150px" height="100px"/>

If you need to leave the data as base64 then just pass the data into $data_content as base64 and use it this way:

<img src="data:image/png;base64,{{ $attachedImage }}" align="right" width="100px" height="100px"/>

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