【问题标题】:Sends email twice on every request (laravel 7.0)每个请求发送两次电子邮件(laravel 7.0)
【发布时间】:2020-10-28 15:18:09
【问题描述】:

大家好,

我正在使用 laravel 7.0。我面临一个错误,每个请求都会发送两次电子邮件。 我一遍又一遍地检查我的代码,但没有发现任何错误。下面我添加代码

这里是控制器方法 控制器获取用户并发送电子邮件

public function __invoke(Request $request)
{
   try{
        $user = User::all()->first();
            
            if($user->language == 'enUS'){
                $emailTemplate = email_template::whereType('information_en')->first();   
            }else if($user->language == 'deDE'){
                $emailTemplate = email_template::whereType('information_de')->first();
            }
            $templateBody = $emailTemplate->body;
            $templateBody = str_replace('#Title#',  $user->title, $templateBody);
            $templateBody = str_replace('#LastName#', $user->lname, $templateBody);

            $data = [];
            $data['email'] = $user->email;
           
            $data['subject'] = $emailTemplate->subject;
            $data['body'] = $templateBody;
            Mail::to($user->email)->send(new EmailSenderMail($data));
            
            return  "<div>Email has been sent to all user.</div>";
    } catch (\Exception $e) {
        
        return $e->getMessage();
    }
    
}

这是邮件类

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class EmailSenderMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $data;
    public function __construct($data){

        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('email.EmailSender')
        ->subject($this->data['subject'])
        ->with([
                'EmailSenderBody' => $this->data['body'],
            ]);
    }
}*

【问题讨论】:

  • but not found any mistake.。你有一个错误你不同意吗?
  • 最可能的原因是请求被重复。我之前使用过 HTML 验证浏览器插件,可能会尝试在隐身模式下重复,看看在没有启用任何插件的情况下是否仍然会发生这种情况

标签: php laravel laravel-5 laravel-7


【解决方案1】:

问题解决了。

如果我使用 Redirect 语句而不是 return 语句,那么我的问题就解决了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-26
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2011-02-04
    • 1970-01-01
    相关资源
    最近更新 更多