问题
Can anyone help me with Laravel Mail function?
I changed config/mail.php file to
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mail.concept.kz'),
'port' => env('MAIL_PORT', 25),
'from' => ['address' => 'support@concept.kz', 'name' => 'asdf'],
'encryption' => env('MAIL_ENCRYPTION', null),
'username' => env('support@concept.kz'),
'password' => env('mypassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
This is my code in controller
Mail::raw($messageBody, function($message) {
$message->from('support@concept.kz', 'Learning Laravel');
$message->to('receiver@mail.ru');
});
if (Mail::failures()) {
echo 'FAILED';
}
return redirect()->back();
I made the same changes to .env file, but nothing happens. Can anyone give me advice what to do? Am I doing something wrong?
回答1:
To send an email from your website
create a view from which the user sends an email:
{!! Form::Open(['url' => 'sendmail']) !!} <div class="col_half"> {!! Form::label('name', 'Name: ' ) !!} {!! Form::text('name', null, ['class' => 'form-control', 'required']) !!} </div> <div class="col_half col_last"> {!! Form::label('email', 'E-Mail: ' ) !!} {!! Form::email('email', null, ['class' => 'form-control', 'required']) !!} </div> <div class="clear"></div> <div class="col_full col_last"> {!! Form::label('subject', 'Subject: ' ) !!} {!! Form::text('subject', null, ['class' => 'form-control', 'required']) !!} </div> <div class="clear"></div> <div class="col_full"> {!! Form::label('bodymessage', 'Message: ' ) !!} {!! Form::textarea('bodymessage', null, ['class' => 'form-control', 'required', 'size' => '30x6']) !!} </div> <div class="col_full"> {!! Form::submit('Send') !!} </div> {!! Form::close() !!}
1a. Create this function in controller:
public function sendMail(Request $request) {
//dd($request->all());
$validator = \Validator::make($request->all(), [
'name' => 'required|max:255',
'email' => 'required|email|max:255',
'subject' => 'required',
'bodymessage' => 'required']
);
if ($validator->fails()) {
return redirect('contact')->withInput()->withErrors($validator);
}
$name = $request->name;
$email = $request->email;
$title = $request->subject;
$content = $request->bodymessage;
\Mail::send('emails.visitor_email', ['name' => $name, 'email' => $email, 'title' => $title, 'content' => $content], function ($message) {
$message->to('your.email@gmail.com')->subject('Subject of the message!');
});
return redirect('contact')->with('status', 'You have successfully sent an email to the admin!');
}
- Create a route to that function in your routes file
create a blade.php view file in /resources/views/emails/visitor_email.blade.php with this content:
<p>Name: {{ $name }}</p> <p>E-Mail: {{ $email }}</p> <p>Subject: {{ $title }}</p> <p>Message: <br> {{ $content }}</p>
in .env file put your data for sending email like this
MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=your.email@gmail.com MAIL_PASSWORD=email-password MAIL_ENCRYPTION=tls
And this should work! if you need more help ask!
来源:https://stackoverflow.com/questions/39838646/laravel-send-email-smtp