dragon88112 2019-02-26 09:56
浏览 41

在laravel中发送附件

I want to send an attachment with email. Till now I am able to send the attachment with mail but the problem is when I am downloading attachment it will show HTML code of my login page.

Please help me to figure it out why attachment show loging page data

Below is my code for compose.blade

<form class="form-horizontal" method="post" action="{{ action('ServiceRequestEmailController@store') }}" enctype="multipart/form-data">
                <div class="mt20 clearfix text-right action-btn-btm">
                    <button class="btn btn-primary">Send</button> &nbsp;
                    <a class="btn btn-danger" href="{{ url('superadmin/service-request/view/' . $ServiceRequest->id) }}">Discard</a>
                    <div class=" border-bottom"></div>
                </div>
                {{ csrf_field() }}
                <input type="hidden" name="sr_id" value="{{ $ServiceRequest->id }}">
                <input type="hidden" name="parent_id" value="{{ $mail->getParentId() }}" />
                <input type="hidden" name="action" value="{{ $mail->getAction() }}" />
                <div class="form-group">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2" for="email">Service Request:</label>
                    <div class="col-sm-8 col-md-6"><a href="{{ url('superadmin/service-request/view/' . $ServiceRequest->id) }}"><span>{{ $ServiceRequest->id }}</span></a></div>
                </div>
                <div class="form-group">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2">Consultant  Name:</label>
                    <div class="col-sm-8 col-md-6"> <span>{{ $ServiceRequest->name }}</span> </div>
                </div>
                <div class="form-group">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2">Consultant  Code:</label>
                    <div class="col-sm-8 col-md-6"> <span>{{ $ServiceRequest->mca_no }}</span> </div>
                </div>
                <div class="form-group">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2" for="from">From:</label>
                    <div class="col-sm-8 col-md-6">
                        <select class="form-control" name="from">
                            @foreach ( $mail->from() as $email )
                                <option>{{ $email }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="form-group autocomplete-to">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2" for="to">To:</label>
                    <div class="col-sm-8 col-md-6">
                        <input type="text" class="form-control" name="to" id="to" value="{{ old('to') ?: $mail->to() }}" />
                        @if ($errors->has('to.*'))
                            <p class="error-msg">{{ $errors->first('to.*') }}</p>
                        @endif
                    </div>
                </div>
                <div class="form-group autocomplete-to">
                    <label class="col-form-label example-text-input  col-sm-4 col-md-2" for="cc">Cc:</label>
                    <div class="col-sm-8 col-md-6">
                        <input type="text" class="form-control" name="cc" id="cc" value="{{ old('cc') ?: $mail->cc() }}" />
                        @if ($errors->has('cc.*'))
                            <p class="error-msg">{{ $errors->first('cc.*') }}</p>
                        @endif
                    </div>
                </div>
                <div class="form-group autocomplete-to">
                    <label class="col-form-label example-text-input  col-sm-4 col-md-2" for="bcc">Bcc:</label>
                    <div class="col-sm-8 col-md-6">
                        <input type="text" class="form-control" name="bcc" id="bcc" value="{{ old('bcc') ?: $mail->bcc() }}" />
                        @if ($errors->has('bcc.*'))
                            <p class="error-msg">{{ $errors->first('bcc.*') }}</p>
                        @endif
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2" for="subject">Subject:</label>
                    <div class="col-sm-8 col-md-6">
                        <input type="text" class="form-control" name="subject" value="{{ old('subject') ?: $mail->getSubject() }}" />
                        @if ($errors->has('subject'))
                            <p class="error-msg">{{ $errors->first('subject') }}</p>
                        @endif
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2" for="template">Email Template:</label>
                    <div class="col-sm-8 col-md-6">
                        <select class="form-control" id="template" v-model="template" v-on:change="loadTemplate()">
                            <option value="">None</option>
                            @foreach ($templates as $template)
                                <option value="{{ $template->id }}">{{ $template->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2" for="content">Email Text:</label>
                    <div class="col-sm-8 col-md-9">
                        <main>
                            <div class="adjoined-bottom">
                                <div class="grid-container">
                                    <div class="grid-width-100">
                                        <textarea name="content" id="editor1" rows="10" cols="80"><?php  echo nl2br($mail->getBody()); ?></textarea>
                                    </div>
                                </div>
                            </div>
                        </main>
                        @if ($errors->has('content'))
                            <p class="error-msg">{{ $errors->first('content') }}</p>
                        @endif
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2" for="email">File Upload:</label>
                    <div class="col-sm-8 col-md-6">
                        <div class="attachments">
                            <input type="file" name="attachments[]" class="form-control" />
                        </div>

                        <div class="clearfix">
                            <button type="button" class="btn btn-primary" data-action="add-more-attachment">Add More</button>
                        </div>
                    </div>
                </div>
                <div>&nbsp;</div>

                @if ($mail->getAction() == 'FORWARD' && ($mailModel = $mail->getInstance()))
                    <div class="gen-info mt20">
                        <div class="col-md-12">
                            <h3 class="subheading icon-open-close" data-toggle="collapse" data-target="#locale-setting">Attachments</h3>
                        </div>
                        <div class="col-md-12 collapse in" id="locale-setting">
                            <div class="clearfix">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>Action</th>
                                            <th>File Name</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @if (($attachments = $mailModel->attachments) && !$attachments->isEmpty())
                                            @foreach ($attachments as $attachment)
                                                <tr>
                                                    <td><a target="_blank" href="{{ url('public/attachments/' . $attachment->filename) }}" target="_blank">Preview</a></td>
                                                    <td>{{ $attachment->original_filename }}</td>
                                                </tr>
                                            @endforeach
                                        @else
                                            <tr>
                                                <td colspan="2" align="center">No Attachments Found</td>
                                            </tr>
                                        @endif
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                @endif

                <div class="form-group">
                    <label class="col-form-label example-text-input col-sm-4 col-md-2" for="email"></label>
                    <div class="col-sm-8 col-md-6">
                        <button class="btn btn-primary">Send</button> &nbsp;
                        <a class="btn btn-danger" href="{{ url('superadmin/service-request/view/' . $ServiceRequest->id) }}">Discard</a>
                    </div>
                </div>
            </form>

and code for controller

 public function compose(ServiceRequest $ServiceRequest)
        {
            $templates = MailTemplate::where('org_id', $ServiceRequest->org_id)->get();
            $mail = new Compose($ServiceRequest);

            return view('admin.ServiceRequestEmails.compose')
                    ->with(compact('ServiceRequest', 'mail', 'templates'));
        }

        public function reply(MailModel $mail)
        {
            $ServiceRequest = $mail->ServiceRequest;
            $templates = MailTemplate::where('org_id', $ServiceRequest->org_id)->get();

            return view('admin.ServiceRequestEmails.compose')
                    ->with([
                        'templates' => $templates,
                        'mail' => new Reply($mail),
                        'ServiceRequest' => $ServiceRequest,
                    ]);
        }

        public function replyToAll(MailModel $mail)
        {
            $ServiceRequest = $mail->ServiceRequest;
    //print_r($ServiceRequest);

            $templates = MailTemplate::where('org_id', $ServiceRequest->org_id)->get();
    //print_r($templates);
    //die();
            return view('admin.ServiceRequestEmails.compose')
                    ->with([
                        'templates' => $templates,
                        'mail' => new ReplyToAll($mail),
                        'ServiceRequest' => $ServiceRequest,
                    ]);
        }

        public function forward(MailModel $mail)
        {
            $ServiceRequest = $mail->ServiceRequest;
            $templates = MailTemplate::where('org_id', $ServiceRequest->org_id)->get();

            return view('admin.ServiceRequestEmails.compose')
                    ->with([
                        'templates' => $templates,
                        'mail' => new Forward($mail),
                        'ServiceRequest' => $ServiceRequest,
                    ]);
        }

        public function store(Request $request)
        {
            $this->request = $request;
            (new \App\Modicare\Validators\ServiceRequestEmailValidator($this->request))
                ->validate();

            $ServiceRequest = ServiceRequest::find($request->sr_id);
            $senderName = $this->getSenderNameByEmail($request->from);

        $this->mail = MailModel::create([
                'sr_id' => $request->sr_id,
                'parent_id' => $request->parent_id,
                'sender_name' => $senderName,
                'from' => $request->from,
                'to' => $request->to,
                'cc' => $request->cc,
                'bcc' => $request->bcc,
                'subject' => $request->subject,
                'body' => $request->content,
                'mca_no' => $ServiceRequest->mca_no,
                'sent_at' => date('Y-m-d H:i:s'),
                'status' => 'SENT',
                'type' => 'OUT'
            ]);

            if ($request->parent_id && ($mail = MailModel::find($request->parent_id))) {
                $mail->status = 'REPLIED';
                $mail->save();
            }

            $mailSender = MailFacade::to(explode(',', $request->to));

            if ($request->cc) {
                $mailSender->cc(explode(',', $request->cc));
            }
            if ($request->bcc) {
                $mailSender->bcc(explode(',', $request->bcc));
            }

            if (!empty($request->attachments) && is_array($request->attachments))
            $files = $this->saveAttachments($request->attachments);
            $this->handleForwardMail();
            $mailSender->send(new ServiceRequestReply($this->mail));
            return redirect('superadmin/service-request/view/' . $request->sr_id);
        }

        private function getSenderNameByEmail($email)
        {
    //        $organizationEmail = OrganizationEmail::where('email', $email) -> orderBy ('id', 'desc')
    //;
            $organizationEmail = OrganizationEmail::where('email', $email)

                        ->first();
                    if ($organizationEmail) {
                        return $organizationEmail->organization->getName();
            }

            return Auth::User()->getName();
        }

        public function view(MailModel $mail)
        {
            if ($mail->status == 'NEW') {
                $mail->status = 'SEEN';
                $mail->save();
            }
            return view('admin.ServiceRequestEmails.view', compact('mail'));
        }

        public function print(MailModel $mail)
        {
            return view('admin.ServiceRequestEmails.print', compact('mail'));
        }

        private function handleForwardMail()
        {
            if ($this->request->action == 'FORWARD' && 
                ($parent = $this->mail->parent)) {

                foreach ($parent->attachments as $attachment) {
                    $newAttachment = $attachment->replicate();
                    $newAttachment->ref_id = $this->mail->id;
                    $newAttachment->save();
                }
            }
        }

        private function saveAttachments(array $attachments)
        {
            $files = [];
            foreach ($attachments as $key => $attachment) {
                $files[$key] = [
                    'newFilename' => $attachment->store('attachments', 'public'),
                    'originalFilename' => $attachment->getClientOriginalName(),
                ];

                Attachment::create($attachment->getRealPath(),[
                    'type' => 'MAIL',
                    'ref_id' => $this->mail->id,
                    'original_filename' => $attachment->getClientOriginalName(),
                    'filename' => basename($files[$key]['newFilename']),
                    'extn' => $attachment->getClientOriginalExtension(),
                    'mime_type' => $attachment->getClientMimeType(),
                    'size' => $attachment->getClientSize(),
                ]);
            }
        }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)