【问题标题】:Laravel-Mail: How to pass ->attachData to viewLaravel-Mail:如何通过 ->attachData 来查看
【发布时间】:2018-01-29 10:03:09
【问题描述】:

我正在尝试在 laravel 邮件中使用内联附件,但似乎很不幸。我也试过这个one,但不能通过嵌入原始数据来工作。我有一个 base 64 图像/png,这里是一个 example

现在我正在尝试使用 attachData,但如何将 ->attachData 传递给我的 mailtransaction.blade。我应该从我的控制器中获取 attachData,但我应该调用什么变量?

控制器.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>

【问题讨论】:

  • 会不会是因为use没有传入$request?
  • @clod986 我编辑了我的问题,是的,它应该在使用中,但我的问题是我怎么能调用它 ->attachData 来传递我的邮件 vue

标签: php laravel laravel-5


【解决方案1】:

您可以将数据作为send() 方法的second 参数传递到您的视图,并且它必须是数据的array。将您的控制器更改为 -

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

在你看来,你可以访问data_contentbase64 as-

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

【讨论】:

    【解决方案2】:

    attachData 方法添加电子邮件附件,不适用于内嵌图像。您需要将图像数据添加到 $data_content 变量并从那里引用它:

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

    然后在您的模板中使用embedData 方法:

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

    如果您需要将数据保留为 base64,则只需将数据作为 base64 传递到 $data_content 并以这种方式使用:

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

    【讨论】:

    • 但我的图像来自我的脚本文件并转换为 base64 它不是典型的图像我只是使用 html2canvas 捕获 div 内容然后将其传递给我的服务器
    • 但您仍在尝试在电子邮件中将其显示为 PNG 文件,对吗?
    • 是的,它应该在电子邮件中显示为 png 图像
    • 如果是这样,那么您将需要使用 embedData 命令。如果您需要将其保留为 base64,您可以使用我对帖子所做的编辑在您的电子邮件中显示内联 base64。
    猜你喜欢
    • 2018-07-29
    • 2019-06-22
    • 2020-09-10
    • 1970-01-01
    • 2020-07-24
    • 2023-03-23
    • 2017-03-18
    • 2014-02-06
    • 1970-01-01
    相关资源
    最近更新 更多