【问题标题】:laravel send mail tolaravel 发送邮件到
【发布时间】:2018-10-05 04:40:59
【问题描述】:

大家晚上好,我有一些问题,发送邮件至: 我在控制器中有一个代码,并且从带有连接的数据库中获取数据,这个 $survey 变量显示给我属于团队的调查电子邮件。 但是我需要一些如何为我从 $survey 变量中获得的所有电子邮件发送电子邮件。当我将 $survey 变量放入 $message->to($survey);它显示了未定义的变量

我的代码如下,我该如何使用它。当我输入 ->to();正常的一个电子邮件地址它可以工作,但我需要向所有团队成员发送相同的电子邮件

public function startSurvey(Request $req) {

    $nameSurvey = $req->input('SurveySelectBox');
    $startDate = $req->input('surveyStartDate');
    $endDate = $req->input('surveyEndDate');

    $data = array(
        'updated_at' => Carbon::now(),
        'started_at' => $startDate,
        'ended_at' => $endDate
    );

    DB::table('survey')->where('surveyId','=',$nameSurvey)->update($data);

    $survey = Survey::where('surveyId' , '=', $nameSurvey)
        ->join('team','team.teamId', '=', 'survey.teamId')
        ->join('teammembersall','teammembersall.TeamId', '=', 'team.TeamId')
        ->join('users','users.id', '=', 'teammembersall.UserId')
        ->select('users.email')
        ->get();

    Mail::raw('You have new survey to answer: http://localhost:8000/profile', function ($message) {
        $message->from('kristijanask@gmail.com', 'New Survey released');
        $message->to($survey);
    });

    return redirect('surveyDrafts');
}

【问题讨论】:

    标签: laravel email message send


    【解决方案1】:

    您需要使用use ($survey) 来使用邮件匿名函数中的变量:

    Mail::raw('...text', function ($message) use ($survey) {
        $message->from('kristijanask@gmail.com', 'New Survey released');
        $message->to($survey);
    });
    

    您可能还需要在查询结果上使用->pluck()toArray(),以使结果成为数组:

    $survey = Survey::where('surveyId' , '=', $nameSurvey)
        // long query
        ->get()
        ->pluck('email')
        ->toArray();
    

    【讨论】:

    • 通过此更正,我收到错误:“非法偏移类型”
    • 知道是哪一部分导致了错误吗?我没有仔细阅读代码。编辑:我选择了mail,但你选择了email。也许就是这样(我打错了)
    • 我已经改成mail了,还是一样。我改为电子邮件,因为在数据库中它的名称为电子邮件。而且我收到了很长的评论,红色标注的地方是: public function addTo($address, $name = null) { $current = $this->getTo(); $当前[$地址] = $名称;返回 $this->setTo($current); }
    • 嗯,可能它不接受 Laravel 的集合作为参数,最后需要调用 toArray:->pluck('email')->toArray()。我也会更新我的答案。
    • 现在完美运行!感谢您的帮助!
    猜你喜欢
    • 2020-01-25
    • 2015-02-26
    • 2017-05-24
    • 1970-01-01
    • 2021-03-28
    • 2018-09-28
    • 2019-09-30
    • 2022-11-15
    • 2018-06-07
    相关资源
    最近更新 更多