使用laravel向客户发送自定义电子邮件

时间:2019-12-26 09:26:26

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

我的客户数据和他们的电子邮件都存储在数据库中。就我而言,我想从客户表中获取用户电子邮件,并向客户发送自定义电子邮件。我是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()函数?我想在电子邮件中显示“谢谢他们”的客户名字。如何使用自定义模板并将其添加到此电子邮件中?如果您能帮助我,我非常感谢。

2 个答案:

答案 0 :(得分:0)

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

您可以在toMail方法中使用类似的方法:

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

当您打电话通知时,您必须通过客户

答案 1 :(得分:0)

使用邮件;

$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);
    });