简体   繁体   English

在 Laravel 5 中发送电子邮件的最佳方式是什么?

[英]What is the best way to sent out email in Laravel 5?

I have a problem sending out email in my Laravel 5 Application.我在 Laravel 5 应用程序中发送电子邮件时遇到问题。


Here is my mail function这是我的邮件功能

email = test@gmail.com电子邮件 = test@gmail.com

Mail::send('emails.activation', array(

  'username'=>$user->username,
  'name'=>$user->name,
  'code'=>$user->code,
  'email'=>$user->email

  ),
function($message){
  $message->from(env('MAIL_USERNAME'),'Site');
  $message->to('email', 'name' )->subject('Site Activation ');
});

I keep getting我不断得到

在此处输入图片说明


Update更新

Mail Configuration in .env file .env文件中的邮件配置

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=donotreply@site.com
MAIL_PASSWORD=***********

New Error新错误

在此处输入图片说明


I'm very curios on what is going on.我对正在发生的事情非常好奇。 What did I do wrong ?我做错了什么 ?

This means there is something wrong in email address fields such as From, Sender or Reply-To fields.这意味着电子邮件地址字段有问题,例如发件人、发件人或回复字段。

Swift Mailer strictly follow RFC standard to avoid emails being caught by spam checker tools. Swift Mailer 严格遵循 RFC 标准,以避免电子邮件被垃圾邮件检查工具捕获。

test@gmail.com doesn't look like a normal email address or does not even exist. test@gmail.com看起来不像一个普通的电子邮件地址,甚至根本不存在。

Try with a different email address example send to me尝试使用不同的电子邮件地址示例发送给我

Mail::send('emails.activation', array(

  'username'=>$user->username,
  'name'=>$user->name,
  'code'=>$user->code,
  'email'=>$user->email

  ),
function($message){
  $message->from('siteEmailaddress@domain.com'),'Site');
  $message->to('avalidEmailaddress@domain.com', 'name' )->subject('Site Activation ');

});

Also if you wish to use gmail as SMTP server below:此外,如果您希望使用 gmail 作为下面的 SMTP 服务器:

'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => 'your-email@gmail.com',
'password' => 'your-password',

EDITED已编辑

NB: MAIL_USERNAME must be your @gmail.com email address eg myname@gmail.com else you will get Connection could not be established with host smtp.gmail.com error注意: MAIL_USERNAME必须是您的@gmail.com电子邮件地址,例如 myname@gmail.com 否则您将收到无法与主机 smtp.gmail.com 建立连接错误

To handle this exception if you can't resolve it, go to open app\\Exceptions\\handler.php如果您无法解决此异常,要处理它,请转到打开 app\\Exceptions\\handler.php

add this inside render method:在渲染方法中添加这个:

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {


        if ($e instanceof Swift_RfcComplianceException){
            //redirect to form
            //You can also delete the user account here if already created or do other stuffs
            return redirect($request->fullUrl())->with('error',"We have issue sending you an email");

        }


        .......
        
    }

NB: Remember to add use Swift_RfcComplianceException;注意:记得添加use Swift_RfcComplianceException; at the top of handler.php在 handler.php 的顶部

It looks like the 'from' address is not valid, you're using an env call currently without knowing 100% what it returns.看起来“发件人”地址无效,您当前正在使用env调用而不知道 100% 返回什么。

Try changing the from address to a real email address for testing purposes.尝试改变from地址到真实的电子邮件地址用于测试目的。

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

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