简体   繁体   English

如何使用 Laravel 发送电子邮件?

[英]How to send email using Laravel?

I'm trying to send an email but I can't move anymore and that's my view:我正在尝试发送电子邮件,但我不能再动了,这就是我的观点:

<h1>Contact TODOParrot</h1>
<form action="contact" method="post">

<div class="form-group">
 <label>Your First Name</label>
 <input type="text" name="Fname" placeholder="Your First Name" />
</div>

<div class="form-group">
  <label>Your Last Name</label>
  <input type="text" name="Lname" placeholder="Your Last Name" />
</div>

<div class="form-group">
<label>Your Email</label>
  <input type="email" name="Email" placeholder="Your Email" />
</div>
<div class="form-group">
<label>Your Phone Number</label>
  <input type="text" name="Phone" placeholder="Your Phone" />
</div>
<div class="form-group">
<label>Your Order</label>
  <input type="text" name="Order" placeholder="Your Order" />
</div>

<div class="form-group">
    <button class="btn btn-default" name="Submit" type="Submit">Send Order</button>
</div>
</form>

and that is my controller :那是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;  
use App\Http\Requests\ContactFormRequest;
    
class aboutController extends Controller
{
    //
    public function create()
    {
        return view('about.contact');
    }

    public function store(ContactFormRequest $request)
    {
        \Mail::send('about.contact',
        array(
            'Fname' => $request->get('Fname'),
            'Lname' => $request->get('Lname'),
            'Email' => $request->get('Email'),
            'Phone' => $request->get('Phone'),
            'Order' => $request->get('Order')
        ), function($message)
    {
        $message->from('mohamedsasa201042@yahoo.com');
        $message->to('elbiheiry2@gmail.com', 'elbiheiry')->subject('TODOParrot Feedback');
    });

        return \Redirect::route('contact')
      ->with('message', 'Thanks for contacting us!');
    }
}

And that's my route:这就是我的路线:

Route::get('contact', 
  ['as' => 'contact', 'uses' => 'AboutController@create']);
Route::post('contact', 
  ['as' => 'contact', 'uses' => 'AboutController@store']);

And that's the configuration in the .env file:这就是 .env 文件中的配置:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=ssl

And I removed the name and password in the question when I press send it gives me 'Forbidden' as a message.当我按下发送时,我删除了问题中的名称和密码,它给了我“禁止”作为消息。

Can anyone help?任何人都可以帮忙吗?

After chatting while with OP, here is the answer.与OP聊天后,这是答案。

The main problem:主要问题:

Your ContactFormRequest.php has the following rules function:您的ContactFormRequest.php具有以下规则功能:

public function rules()
    {
        return [
        'name'    => 'required',
        'email'   => 'required|email',
        'message' => 'required',
        ];
    }

But your form does not have name and messages, so you need to delete not existing elements or modify them if required, for my testing purpose I did only kept email:但是您的表单没有名称和消息,因此您需要删除不存在的元素或根据需要修改它们,出于我的测试目的,我只保留了电子邮件:

public function rules()
    {
        return [
            'Email' => 'required|email',
        ];
    }

And it is a good practice to keep name conventions like if you use Email with capital E than use Email every where.保持命名约定是一种很好的做法,例如,如果您使用带有大写E电子邮件,而不是在任何地方都使用电子邮件。

Therefore the form was never submitted to be send.因此,从未提交过该表单以供发送。

I suggest also you structure your store function which I have did and test and it works, you can modified it to fit your requirement:我还建议您构建我已经做过并测试过的store功能,它可以工作,您可以修改它以满足您的要求:

$data = [
            'no-reply' => 'contact-from-web@nomail.com',
            'admin'    => 'mohamedsasa201042@yahoo.com',
            'Fname'    => $request->get('Fname'),
            'Lname'    => $request->get('Lname'),
            'Email'    => $request->get('Email'),
            'Phone'    => $request->get('Phone'),
            'Order'    => $request->get('Order'),
        ];

        \Mail::send('about.contact', ['data' => $data],
            function ($message) use ($data)
            {
                $message
                    ->from($data['no-reply'])
                    ->to($data['admin'])->subject('Some body wrote to you online')
                    ->to($data['Email'])->subject('Your submitted information')
                    ->to('elbiheiry2@gmail.com', 'elbiheiry')->subject('Feedback');
            });

and it should works,它应该有效,

I have test it only with Mandrill API email service, but you can give it a try with SMTP or API, it is up to you.我仅使用 Mandrill API 电子邮件服务对其进行了测试,但您可以尝试使用 SMTP 或 API,这取决于您。

If you want to make an email confirmation, you need to create email confirmation view forward your data to it like following:如果要进行电子邮件确认,则需要创建电子邮件确认视图,将数据转发给它,如下所示:

\Mail::send('about.emailconfirmation', ['data' => $data],

and your view could looks like this:您的视图可能如下所示:

<tr>
    <td>
        <h1>Contact form</h1>
        <p>Dear {{ $data['Fname'] }},</p>
        <p>Thank you for contacting me.</p>
        <p>I will respond to your inquiry as quickly as possible.</p>
        <hr/>
        <p><b>Provided email</b></p>
        <p>Email: {{ $data['Email'] }},</p>
    </td>
</tr>

This is only example but you can further modify it.这只是示例,但您可以进一步修改它。

As per my understanding, I think you are missing authorize part in your ContactFormRequest Goto your this App/Http/Request/ContactFormRequest and you will this method根据我的理解,我认为您在ContactFormRequest中缺少authorize部分转到您的此App/Http/Request/ContactFormRequest并且您将使用此方法

public function authorize()
{
    return false;
}

so just set return as true.所以只需将 return 设置为 true。 It will allow further process.它将允许进一​​步的处理。

public function authorize()
{
    return true;
}

Edited已编辑

change your Contact POST Route to this将您的Contact POST Route更改为此

Route::post('contact/store',['as' => 'contact-store', 'uses' => 'AboutController@store']);

and in your form action, just change this.在您的表单操作中,只需更改此设置即可。

<form action="contact/store" ........
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls

i had some similar problem and changed the MAIL_DRIVER = sendmail from smtp我有一些类似的问题,并从 smtp 更改了 MAIL_DRIVER = sendmail

Laravel app Laravel 应用程序

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465          
MAIL_USERNAME=<<your email address>>
MAIL_PASSWORD=<<app password>>

MAIL_ENCRYPTION= ssl   

In your Controller Setting在您的控制器设置中

faced面对

use Illuminate\Support\Facades\Mail;

to Send mail发送邮件

$to_name = "RECEIVER_NAME";
$to_email = "tomail@gmail.com";
$data = array("name"=>"Cloudways (sender_name)", "body" => "A test mail");

Mail::send([], $data, function($message) use ($to_name, $to_email) {
$message->to($to_email, $to_name)
->subject("Laravel Test Mail");
$message->from("preealweb@gmail.com","Test Mail");
});

Note: from mail and to mail注意:从邮件和到邮件

make sure enable SucureLess mail in gmail setting确保在 gmail 设置中启用SucureLes 邮件

Here are three methods to send email in laravel 8.
First one is through our email id.
Second one is through Mailgun.
Third one is through SendinBlue.


# For smtp

# MAIL_MAILER=smtp
# MAIL_HOST=smtp.gmail.com
# MAIL_PORT=587
# MAIL_USERNAME=xxxxxxxxxxxxx@gmail.com
# MAIL_PASSWORD=xxxxxxxxxxxxxxx
# MAIL_ENCRYPTION=tls
# MAIL_FROM_ADDRESS=xxxxxxxxxxx@gmail.com
# MAIL_FROM_NAME="${APP_NAME}"

# For Mailgun

# MAIL_MAILER=mailgun
# MAIL_HOST=smtp.mailgun.org
# MAIL_PORT=587
# MAIL_USERNAME=sandboxxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org
# MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# MAIL_ENCRYPTION=tls
# MAIL_FROM_ADDRESS=xxxxxxxxxxxxxxxxxxxxxxxxx
# MAIL_FROM_NAME="${APP_NAME}"
# MAILGUN_SECRET=API private key
# MAILGUN_DOMAIN=sandboxxxxxxxxxxxxxxxxxxxxx.mailgun.org

# For sendinblue
# MAIL_DRIVER=smtp
# MAIL_HOST=smtp-relay.sendinblue.com
# MAIL_PORT=587
# MAIL_USERNAME=xxxxxxxxxxxxxxxxxxxxxx@gmail.com
# MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxx
# MAIL_ENCRYPTION=tls
# MAIL_FROM_ADDRESS=xxxxxxxxxxxxxxxxxxxxxx@gmail.com
# MAIL_FROM_NAME="${APP_NAME}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM