Laravel 发送邮件 SMTP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39838646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:33:04  来源:igfitidea点击:

Laravel send email SMTP

phplaravelemailsmtp

提问by Yersultan

Can anyone help me with Laravel Mail function?

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

I changed config/mail.phpfile to

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

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

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

回答by lewis4u

To send an email from your website

从您的网站发送电子邮件

  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() !!}
    
  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() !!}
    

1a. Create this function in controller:

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('[email protected]')->subject('Subject of the message!');
        });


    return redirect('contact')->with('status', 'You have successfully sent an email to the admin!');

}
  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.php with this content:

      <p>Name: {{ $name }}</p>
      <p>E-Mail: {{ $email }}</p>
      <p>Subject: {{ $title }}</p>
      <p>Message: <br>
      {{ $content }}</p>
    
  3. 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
    
  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. 在 .env 文件中放置您的数据以发送这样的电子邮件

    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!

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