Laravel 发送邮件 SMTP

Yer*_*tan 4 php email smtp laravel

任何人都可以帮助我使用Laravel 邮件功能吗?

我将config/mail.php文件更改为

'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',
Run Code Online (Sandbox Code Playgroud)

这是我在控制器中的代码

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();
Run Code Online (Sandbox Code Playgroud)

我对.env文件进行了相同的更改,但没有任何反应。谁能给我建议该怎么做?难道我做错了什么?

lew*_*s4u 5

从您的网站发送电子邮件

  1. 创建用户发送电子邮件的视图:

    {!! 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() !!}
    
    Run Code Online (Sandbox Code Playgroud)

1a. 在控制器中创建此函数:

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!');

}
Run Code Online (Sandbox Code Playgroud)
  1. 在路由文件中创建到该函数的路由
  2. 在 /resources/views/emails/visitor_email.blade.php 中创建一个 Blade.php 视图文件,内容如下:

      <p>Name: {{ $name }}</p>
      <p>E-Mail: {{ $email }}</p>
      <p>Subject: {{ $title }}</p>
      <p>Message: <br>
      {{ $content }}</p>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在 .env 文件中,将您的数据用于发送这样的电子邮件

    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=587
    MAIL_USERNAME=your.email@gmail.com
    MAIL_PASSWORD=email-password
    MAIL_ENCRYPTION=tls
    
    Run Code Online (Sandbox Code Playgroud)

这应该有效!如果您需要更多帮助,请询问!