php – Laravel不从地址发送

我想在用户查看页面时向我的电子邮件发送电子邮件.

我成功完成了.

我的代码看起来像这样:

Route::any('contact_us/send', function() {
    return Mail::send('contactus.send', ['name' => 'Advaith'], function($message) {
        $name = $_POST['name']; 
        $from = $_POST['email'];
        $message->to('itsme@gmail.com')->from($from, $name)->subject('Feed Back'); 
        //itsme@gmail.com is used in this question only
    });
});

此代码将电子邮件发送到我的帐户.

但它不是发送发件人姓名和电子邮件($name和$from)
我甚至尝试更改变量并给它一个示例电子邮件.

我的.env文件如下所示:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=itsme@gmail.com
MAIL_PASSWORD=**********
MAIL_ENCRYPTION=tls

我的config / mail.php文件如下所示:

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
    'address' => 'hello@example.com',
    'name' => 'Example',
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,

我发送数据的表单是这样的:

<form class="ad-form col-md-12" method="POST" action="/contact_us/send">
    {{ csrf_field() }}
    <input placeholder="Name...*" type="text" name="name" required="required">
    <br>
    <input placeholder="Email...*" type="email" name="email" required="required">
    <br>
    <textarea required="required" placeholder="Your valuable feedback...*" name="comment" rows="5" cols="40"></textarea>
    <br>
    <input type="submit" name="submit" value="Send">
</form>

我的contactus / send.blade.php文件如下所示:

<div style="border: 2px solid orange; padding: 30px;">
    <div>From: <b>{{ $_POST['name'] }}</b></div>
    <div>From-Email: <b>{{ $_POST['email'] }}</b><hr></div><br>
    <div style="text-align: justify;"><strong>{{ $_POST['comment'] }}</strong></div>
</div>

请告诉我为什么它不发送来自地址.

还请告诉我如何在发送电子邮件后转到另一页.

最佳答案 这个问题不是Laravel问题.你在评论中说,你写的是你发给自己的电子邮件.这是因为gmail不允许从地址中随机使用,以防止发送垃圾邮件.

您必须先注册备用地址,然后才能将它们用作“来自地址”,否则gmail会将其更改回默认地址.

有关参考,请参阅herehere

还有一些SO资源来处理这个问题:

How to change from-address when using gmail smtp server,

When using Gmail for SMTP, can you set a different “from” address?

change sender address when sending mail through gmail in c#

点赞