【问题标题】:send custom emails to cutomers with laravel使用 laravel 向客户发送自定义电子邮件
【发布时间】:2020-04-16 13:48:05
【问题描述】:

我的数据库中有客户数据和他们的电子邮件。就我而言,我想从客户表中获取用户电子邮件并向客户发送自定义电子邮件。我是 laravel 的新手,这是我第一次使用 laravel 通知。帮我做这件事!

我已经创建了这个 SendMailController:

    public function sendNotifications(Request $request, $id)
    {
        $id = $request->id;
        
        $customer_id = DB::table('jobs')->select('customers_id')->where('id', '=', $id)->get();
        $customer_email = DB::table('customer')->select('email')->where('id', '=', $customer_id)->get();
        $customer_firstname = DB::table('customer')->select('firstname')->where('id', '=', $customer_id)->get();

       sendEmail($customer_firstname, $customer_email);

 
       return view('admin.sendNotifications');
    }

我创建了一个 sendEmail 通知类:

class sendEmail extends Notification
{
    use Queueable;

    public function __construct($customer_firstname, $customer_email)
    {
        $this->customer_email = $customer_email;
        $this->customer_firstname = $customer_firstname;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->error()
                    ->subject('Thank you!')
                    ->from('hashankannangara@gmail.com', 'Sender')
                    ->to($this->customer_email)
                    ->line($this->customer_firstname.''.'Thanks for got services from us!');
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

如何将客户电子邮件传递给 to() 函数?我想在电子邮件中显示客户的名字并感谢他们。如何使用自定义模板并添加到此电子邮件?如果您能帮助我,我将不胜感激。

【问题讨论】:

    标签: php laravel email php-7 laravel-5.8


    【解决方案1】:

    https://laravel.com/docs/6.x/notifications#mail-notifications

    你可以在 toMail 方法中使用这样的东西:

    return (new CustomMail($this->customer))->to($this->customer->email);
    

    当你打电话通知时,你必须通过一个客户

    【讨论】:

    • 我已经编辑了我的代码。那是对的吗?我做了类似你的事情。
    • 这样有效吗?如果您为此创建一个专用邮件类,那可能是最好的
    • 不,我有一个错误。发送邮件控制器中的Object of class stdClass could not be converted to string
    • 你没有在控制器中调用 notify。您只需键入 sendEmail。找到cutomer,然后像这样调用它 $cutomer->notify(new SendEmail($customer))
    • Object of class stdClass could not be converted to string 出现这个错误是因为这个原因吗?
    【解决方案2】:

    使用邮件;

    $data = 'set data variable pass in view' 
    $email= 'receiver' email'
    
    Mail::send('here set your file for view (ex. email.access(blade))', $data, function ($message) use ($email) {
            $message->from('sender mail','Title')->subject('Register');
            $message->to($email);
        });
    

    【讨论】:

      猜你喜欢
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 2021-01-08
      相关资源
      最近更新 更多