dane5890's avatar

Send A Livewire Email With A File Attachment

Hi, working on some contact forms for a new salon site. The email contact forms work fine with Livewire, but there is no file attachment in my email.

<?php

namespace App\Http\Livewire\Contact;

use Livewire\Component;
use Mail;
use Livewire\WithFileUploads;

class Stylistemail extends Component
{

 use WithFileUploads;

  public $name;
  public $email;
  public $comment;
  public $phone;
  public $success;
  public $attachment;
  protected $rules = [
      'name' => 'required',
      'email' => 'required|email',
      'phone' => 'required',
      'comment' => 'required|min:5',
      'attachment' => 'required|mimes:jpg,jpeg,png,pdf,doc,docx',
  ];

  public function stylistcontactFormSubmit()
  {
      $contact = $this->validate();

      Mail::send('stylist_email',
      array(
          'name' => $this->name,
          'email' => $this->email,
          'phone' => $this->phone,
          'attachment' => $this->attachment,
          'comment' => $this->comment,
          ),
          function($message){
              $message->from('[email protected]');
              $message->to('[email protected]', 'Mz Black Magic')->subject('New Hair Stylist Resume');
          }
      );

      $this->success = 'Thank you for reaching out to The Beauty Lounge. We will get back to you shortly.';

      $this->clearFields();
  }

  private function clearFields()
  {
      $this->name = '';
      $this->email = '';
      $this->comment = '';
      $this->phone = '';
      $this->attachment = '';
  }

    public function render()
    {
        return view('livewire.contact.stylistemail');
    }
}


and my blade file looks something like this

<div>

  	<div class="bg-contact2">
  		<div class="container-contact2">
  			<div class="wrap-contact2">
  				<form class="contact2-form validate-form" wire:submit.prevent="stylistcontactFormSubmit" action="/appointments" method="POST" enctype="multipart/form-data">
  					<span class="contact2-form-title loc-email-header">
  						Join The Stylist Team
  					</span>

            @if ($success ?? '')

            <div class="row">

          <div class="col-md-4">

            <img src="{{ asset('images/logos/natural-call.png') }}" style="height: 200px;" alt="">

          </div>

          <div class="col-md-8">

            <div class="alert alert-success" role="alert">
            {{ $success ?? '' }}
          </div>

          </div>

            </div>


            @endif

  					<div class="wrap-input2 validate-input" data-validate="Name is required">

              @error('name')
                  <p class="" style="color: crimson;">{{ $message }}</p>
              @enderror

  						<input wire:model="name" class="input2" type="text" name="name" value="{{ old('name') }}">
  						<span class="focus-input2" data-placeholder="NAME"></span>
  					</div>

  					<div class="wrap-input2 validate-input" data-validate = "Valid email is required: [email protected]">

              @error('email')
                  <p class="" style="color: crimson;">{{ $message }}</p>
              @enderror

  						<input wire:model="email" class="input2" type="text" name="email" value="{{ old('email') }}">
  						<span class="focus-input2" data-placeholder="EMAIL"></span>
  					</div>

            <div class="wrap-input2 validate-input" data-validate = "Valid email is required: [email protected]">

              @error('phone')
                  <p class="" style="color: crimson;">{{ $message }}</p>
              @enderror

              <input wire:model="phone" class="input2" type="tel" name="phone" value="{{ old('phone') }}">
              <span class="focus-input2" data-placeholder="Phone Number"></span>
            </div>

            <div class="wrap-input2 validate-input" data-validate = "Valid email is required: [email protected]">

              @error('attachment')
                  <p class="" style="color: crimson;">{{ $message }}</p>
              @enderror

              <input wire:model="attachment" class="input2" type="file" name="attachment" value="">
              <span class="focus-input2" data-placeholder="Upload Your Resume"></span>
            </div>

  					<div class="wrap-input2 validate-input" data-validate = "Message is required">

              @error('comment')
                  <p class="" style="color: crimson;">{{ $message }}</p>
              @enderror

  						<textarea wire:model="comment" class="input2" name="comment">{{ old('comment') }}</textarea>
  						<span class="focus-input2" data-placeholder="MESSAGE"></span>
  					</div>

  					<div class="container-contact2-form-btn">
  						<div class="wrap-contact2-form-btn">
  							<div class="contact2-form-bgbtn"></div>
  							<button class="contact2-form-btn" id="send-btn" type="submit">
  								Send Your Message
  							</button>
  						</div>
  					</div>
  				</form>

          @if ($success ?? '')

          <div class="row">

        <div class="col-md-4">

          <img src="{{ asset('images/logos/natural-call.png') }}" style="height: 200px;" alt="">

        </div>

        <div class="col-md-8">

          <div class="alert alert-success" role="alert">
          {{ $success ?? '' }}
        </div>

        </div>

          </div>


          @endif

  			</div>
  		</div>
  	</div>


  </div>



</div>


Any help would be appreciated.

0 likes
1 reply
Snapey's avatar

i would imagine the problem stems from passing the uploaded file object to the attach function and not the path to the file. You possibly need to store the file first

Please or to participate in this conversation.