Laravel发送电子邮件SMTP

Laravel发送电子邮件SMTP

本文介绍了Laravel发送电子邮件SMTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以使用 Laravel Mail功能帮助我吗?

Can anyone help me with Laravel Mail function?

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

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'mail.concept.kz'),
'port' => env('MAIL_PORT', 25),
'from' => ['address' => '[email protected]', 'name' => 'asdf'],
'encryption' => env('MAIL_ENCRYPTION', null),
'username' => env('[email protected]'),
'password' => env('mypassword'),
'sendmail' => '/usr/sbin/sendmail -bs',

这是我在控制器中的代码

This is my code in controller

Mail::raw($messageBody, function($message) {
            $message->from('[email protected]', 'Learning Laravel');
            $message->to('[email protected]');
        });

        if (Mail::failures()) {
            echo 'FAILED';
        }
        return redirect()->back();

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

I made the same changes to .env file, but nothing happens.Can anyone give me advice what to do? Am I doing something wrong?

推荐答案

要从您的网站发送电子邮件

To send an email from your website


  1. 创建一个视图,用户可以通过该视图发送电子邮件:

  1. 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。在控制器中创建以下函数:

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('[email protected]')->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视图文件内容:

  1. Create a route to that function in your routes file
  2. create a blade.php view file in /resources/views/emails/visitor_email.blade.phpwith this content:

  <p>Name: {{ $name }}</p>
  <p>E-Mail: {{ $email }}</p>
  <p>Subject: {{ $title }}</p>
  <p>Message: <br>
  {{ $content }}</p>


  • 在.env文件中,您的数据用于像这样发送电子邮件

  • in .env file put your data for sending email like this

    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=587
    [email protected]
    MAIL_PASSWORD=email-password
    MAIL_ENCRYPTION=tls
    


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

    And this should work! if you need more help ask!

    这篇关于Laravel发送电子邮件SMTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-24 01:33