vemcogroup / laravel-sparkpost-driver

SparkPost driver to use with Laravel
MIT License
40 stars 16 forks source link

Add support for X-MSYS-API header #27

Closed NielsJanssen closed 1 year ago

NielsJanssen commented 1 year ago

This adds support for the X-MSYS-API header, in addition to the X-MSYS-SUBACCOUNT header. All headers are passed to Sparkpost directly to also preemptively include support for future additions by Sparkpost. The previous subaccount_id header implementation is replaced, but kept for backwards compatibility.

This fixes #26

basvandertogt commented 1 year ago

Tried your commit but it seems not to work.

<?php

namespace App\Mail;

use App\Models\Message;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Headers;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Content;

class CustomerSatisfaction extends Mailable
{
    use Queueable, SerializesModels;

    public $message;
    public $subject;
    public $reply_to;
    public $from_name;
    public $body;

    /**
     * Create a new message instance.
     *
     * @param Message $message
     * @param string $subject
     * @param string $reply_to
     * @param string $from_name
     * @param string $body
     */
    public function __construct(Message $message, $subject, $reply_to, $from_email, $from_name, $body)
    {
        $this->message = $message;
        $this->subject = $subject;
        $this->reply_to = $reply_to;
        $this->from_email = $from_email;
        $this->from_name = $from_name;
        $this->body = $body;
    }

    /**
     * Get the message headers.
     *
     * @return \Illuminate\Mail\Mailables\Headers
     */
    public function headers()
    {
        $options = [
            'metadata' => [
                'team_id' => (string)$this->message->team_id,
                'user_id' => (string)$this->message->user_id,
                'message_id' => (string)$this->message->id
            ]
        ];

        return new Headers(text: [
            'X-MSYS-API' => json_encode($options)
        ]);
    }

    /**
     * Get the message envelope.
     *
     * @return \Illuminate\Mail\Mailables\Envelope
     */
    public function envelope()
    {
        return new Envelope(
            from: new Address($this->from_email, $this->from_name),
            to: $this->message->email,
            replyTo: $this->reply_to ?: null,
            subject: $this->subject
        );
    }

    /**
     * Get the message content definition.
     *
     * @return \Illuminate\Mail\Mailables\Content
     */
    public function content()
    {
        return new Content(
            markdown: 'emails.customer_satisfaction',
            with: [
                'body' => $this->body
            ],
        );
    }
}
makije commented 1 year ago

This package is is using https://developers.sparkpost.com/api/transmissions/ As far as I can tell, you are trying to implement features for the https://developers.sparkpost.com/api/smtp/

Which is two separate ways of sending the email. If you want to use the smtp variant, then this package is not needed, setup your project to use the smtp driver, then the headers parts of the message gets passed. Most admit, I have not tried it myself.

Or is there something I am not seeing?

basvandertogt commented 1 year ago

I don't understand. This package is required. Otherwise i get this error: Unsupported mail transport [sparkpost]. What is the correct way to send an email with this package and use X-MSYS-API header?

makije commented 1 year ago

We agree you are trying to use this https://developers.sparkpost.com/api/smtp/#header-using-the-x-msys-api-custom-header

If that is correct, then you need to setup the smtp mail driver according to these instructions https://developers.sparkpost.com/api/smtp/#header-client-configuration

basvandertogt commented 1 year ago

A got it. Thank you very much. It's working now!

makije commented 1 year ago

You are very welcome.