Pixelairport's avatar

Use mailable with (mailjet) API

Hi. I wanted to send mails with mailjet and mailjet templates. Mailjet offers a way to design templates in the service, that no developer is needed for changes. So I dont need the views and design to make in laravel. I only send an ID to Mailjet, which template to use and send also variables if needed. My question is, is it still ok to use mailables? I did a check and it work. And maybe you say, yes this is the right way, but I wanted to know that it is not wrong or you say "dont do it, because of...". This is what I have and works:

class TestMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function send($mailer)
    {
        $mj = Mailjet::getClient();
        $body = [
            'FromEmail' => "[email protected]",
            'FromName' => "John",
            'Subject' => "My Testmail",
            'MJ-TemplateID' => 1234567,
            'Recipients' => [['Email' => "[email protected]"]]
        ];
        $response =  $mj->post(Resources::$Email, ['body' => $body]);
        $response->success() && var_dump($response->getData());
    }
}

For me it makes sense to use Mailables, because I have an overview and when I change someday I can change all at once instead of searching my mail code in the whole code. Is it ok to overwrite the send method?

0 likes
5 replies
Pixelairport's avatar

@Sinnbeck Seems that is like the original mailjet package I already use https://github.com/mailjet/laravel-mailjet ... I also can do everything, what is in the readme file. I just wanted the best way to keep the code clean and also want the extras of mailables like queue.

Also what you say would work, but I dont want to use the normal way with views. I want to use the mailjet template service, that a marketing team can setup everything on their own, without a develoer.

PS: You updated and also say mailjet/laravel-mailjet ... I cant find an example how to combine it with mailables. Maybe i just write some classes and test it. Then Im not sure if it is the best practice, but when it work i will be fine with it. I just hoped there is somebody who had already thought about this problem.

Pixelairport's avatar

@Sinnbeck Yes I had to write something for my workaround, but when I think back, you helped me often with other problems :) thx for your help

Pixelairport's avatar
Pixelairport
OP
Best Answer
Level 12

For all with the same idea: It works. You can create a Mailable and override the send method. I wrote a trait i use in my mailable, which offers the function to send via API the mail. The good thing is that ShouldQueue works. First I thougt it dont, but I had first to setup Jobs in laravel and then switch from sync to database in .env. This is only a tip for people who also think it will not work. Now I can use the templates at mailjet (others services will also work I think) and can seperate the developer work from marketing work. Hope that helps.

Please or to participate in this conversation.