jdavidbakr / mail-tracker

Package for Laravel to inject tracking code into outgoing emails.
MIT License
562 stars 129 forks source link

inserting user_id value when saving SentEmail model #235

Closed Khuthaily closed 6 months ago

Khuthaily commented 6 months ago

@jdavidbakr, thank you for this great package. I have a working email app; all is good. I have added this packaged to track sent emails, and I customized the sent_mails table to include user_id column.

My question is: how can I add the user_id attribute as the email gets saved in the database if my (sample) code is like this:

Controller

$user = User::find(1);
Mail::mailer('smtp')
    ->to($user->email)
    ->send(new SayHiToUser($user));

Mail

protected $user;

public function __construct($user)
{
    return $this->user = $user;
}

public function envelope(): Envelope
{
    return new Envelope(
        from: new Address('email@sample.net', 'Sender'),
        subject: 'Hello There',
    );
}
public function content(): Content
{
    return new Content(
        view: 'path.to.view',
        with: ['user' => $this->user]
    );
}
jdavidbakr commented 6 months ago

If you need to add additional data to the model, you can use a listener or pass data to the existing listeners via https://github.com/jdavidbakr/mail-tracker?tab=readme-ov-file#passing-data-to-the-event-listeners