Laravel发送电子邮件SMTP

时间:2016-10-03 19:02:56

标签: php laravel email smtp

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

我将 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',

这是我在控制器中的代码

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();

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

1 个答案:

答案 0 :(得分:2)

从您的网站发送电子邮件

  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() !!}
    
  2. 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!');
    
    }
    
    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>
      
    3. .liv文件中的
    4. 将您的数据用于发送此类电子邮件

      MAIL_DRIVER=smtp
      MAIL_HOST=smtp.gmail.com
      MAIL_PORT=587
      MAIL_USERNAME=your.email@gmail.com
      MAIL_PASSWORD=email-password
      MAIL_ENCRYPTION=tls
      
    5. 这应该有效!如果你需要更多帮助,请问!