jdavidbakr / mail-tracker

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

The tracker does not store the record in the sent_emails table #199

Closed dekts closed 1 year ago

dekts commented 1 year ago

The tracker stores records when using the default .env mail transport, but not when using the notification handler using dynamic mail transport to send mail using the mailer class from Laravel.

I am using below code to change mail transport dynamically.

$transport = (new \Swift_SmtpTransport(config('mail.mailers.smtp.host'), config('mail.mailers.smtp.port')))
    ->setusername(config('mail.mailers.smtp.username'))
    ->setPassword(config('mail.mailers.smtp.password'))
    ->setEncryption(config('mail.mailers.smtp.encryption'));

\Mail::setSwiftMailer(new \Swift_Mailer($transport));

then in toMail method of Notification:

public function toMail($notifiable)
{
        $message = (new Common($this->booking, $this->getSubject(), $this->getBody()));

        return $message;
}

Using toMail I am sending the email to my customers and here the Common Class is my Mail.

class Common extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public function build()
    {
        if (isset($this->data['replyTo']))
            $this->replyTo($this->data['replyTo']['email'], $this->data['replyTo']['name']);

        return $this->to([$this->data['custEmail']])
            ->subject($this->subject)
            ->view('partials.email.body', ['body' => $this->body]);
    }

Could this be related to a configuration issue or a package bug? Do I need to modify the code anywhere to make the mail tracker work?

jdavidbakr commented 1 year ago

If you launch your own swift mail transport, you will have to manually register the plugin. The service provider does that for you so it works when you use the Laravel mail methods, but in your case you are creating your own transport - so look at the service provider to see how the plugin is registered, you should be able to add that to your code.