我想通过电子邮件发送附件。到目前为止,我可以通过邮件发送附件,但是问题是,当我下载附件时,附件将显示我的登录页面的HTML代码。
请帮助我弄清楚为什么附件显示日志页面数据
下面是我的 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>
<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> </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>
<a class="btn btn-danger" href="{{ url('superadmin/service-request/view/' . $ServiceRequest->id) }}">Discard</a>
</div>
</div>
</form>
和控制器
的代码 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(),
]);
}
}