0

I am new to Laravel. I am trying to send email with attachments

here what I try.

in my app/config/mail

'driver' => 'mail',

this is my Route

Route::get('join-us', 'handleFormController@joinus');
Route::post('joinRequest', 'handleFormController@getJoinRequest');

and this is the form

{{ Form::open(array('url'=>'joinRequest', 'files'=>'true')) }}

                    @foreach($errors->all('
                    :message
                    ') as $message) {{ $message }}
                    @endforeach

                    <div class="form-group pull-right ">
                        {{ Form::label('name', ' ') }}
                        <div class="col-xs-12">
                            {{ Form::text('name', '', array('class'=>'form-control1 row input-sm', 'placeholder'=>'الاسم')) }}
                        </div>
                    </div>
                    <div class="form-group pull-right ">
                        {{ Form::label('email', ' ') }}
                        <div class="col-xs-12">
                            {{ Form::text('email', '', array('class'=>'form-control1 row input-sm', 'placeholder'=>'البريد الإلكتروني')) }}
                        </div>
                    </div>
                    <div class="form-group pull-right ">
                        {{ Form::label('job', ' ') }}
                        <div class="col-xs-12">
                            {{ Form::text('job', '', array('class'=>'form-control1 row input-sm', 'placeholder'=>'المهنة او الوظيفة الحاليه')) }}
                        </div>
                    </div>
                    <div class="form-group pull-right ">
                        {{ Form::label('comment', ' ') }}
                        <div class="col-xs-12">
                            {{ Form::textarea('comment', '', array('class'=>'form-control1 row input-sm', 'placeholder'=>'اترك تعليق')) }}
                        </div>
                    </div>
                    {{--<div class="form-group pull-right ">
                        {{ Form::label('resume', ' ') }}
                        <div class="col-xs-12">
                            {{ HTML::script('siteroot/fileinput.js') }}
                            {{ Form::file('resume', array('class'=>'file')) }}
                        </div>
                    </div>--}}
                    <div class="col-xs-12  pull-right">
                        {{ Form::submit('أرسال', array('class'=>'btn btn-primary pull-right')) }}
                    </div>
                    {{Form::close()}}

and here is my controller

public function getJoinRequest()
    {
        $data = Input::all();
        $rules = array(
            'name' => 'required|regex:/^[\pL\s\-]+$/u',
            'email' => 'required|email',
            'job' => 'required|alpha',
            'comment' => 'required|min:25'
        );

        $validator = Validator::make($data, $rules);
        if ($validator->passes()) {
            Mail::send('emails.joinRetunMessage.hello', $data, function ($message) use ($data) {
                $message->from($data['email'], $data['name']);
                $message->to('johnef_sh@hotmail.com', 'El Arabia')->subject('Join us request');
                /*$message->attach($data['resume']->getRealPath(), array(
                        'as' => 'resume.' . $data['resume']->getClientOriginalExtension(),
                        'mime' => $data['resume']->getMimeType())
                );*/
            });
            return Redirect::to('/join-us')
                ->with('message', 'Your message has been sent. Thank You!');
        } else {
            return Redirect::to('/join-us')->withErrors($validator);
        }
    }

when I submit the form it returns to page without any errors, but the mail was not sent.

Yousef Altaf
  • 2,631
  • 4
  • 46
  • 71

1 Answers1

1

I use following code for sending temporary password in case of forgot password

public function postForgotPassword(ForgotPasswordRequest $request){

$email=$request->email;
 $objUser=DB::table('users')
                  ->where('email','=',$email)
                  ->select('email','id','first_name','last_name','user_group_id')
                  ->first();

$string = str_random(15);
$pass=\Hash::make($string);

$objUser2=User::find($objUser->id);
$CURRENT_TIMESTAMP=new DateTime();
$objUser2->temporary_pass=$pass;
$objUser2->pass_status=1;
$objUser2->updated_at=$CURRENT_TIMESTAMP;
$objUser2->save();            

$data=array("email"=>$email,"pass"=>$string,"first_name"=>$objUser->first_name,"last_name"=>$objUser->last_name);
          $email=array("email"=>$email);
          Mail::send('emails.forgot_password',$data, function($message) use($email) {
          $message->to($email['email'],'MoveInSpace')->subject('Password Recovery ');
          });

          return Redirect::to('auth/login')->with('flash_message','Check your email account for temporary password');
}

And I write email template in forgot_password.blade.php which is located in view.emails folder. Also make sure you configure config mail file properly. and if possible use smtp driver

'driver' => 'smtp',

I use following code in my mail.php

'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => 'abc@gmail.com', 'name' => 'ABC'),
'encryption' => 'tls',
'username' => 'abc@gmail.com',
'password' => 'yourpassword',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
Nil
  • 513
  • 3
  • 16