Laravel provides the possibility to implement your own mail transport in order to send email over APIs that they aren't implemented in the framework itself by extending Illuminate\Mail\Transport\Transport. I have done so for the gmail API as this provides some benefits over sending over SMTP. There is however a scenario which is giving me some difficulties: the API requires you to impersonate a given user to send mail as that user. Currently, I'm using the 'from' address to perform that action.
use Illuminate\Mail\Transport\Transport;
use Google\Client as GoogleClient;
class MailTransport extends Transport
{
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$client = new GoogleClient;
// initialise the client further, code omitted
$client->setSubject(
array_key_exists(0, array_keys($message->getFrom()))
? array_keys($message->getFrom())[0]
: self::DEFAULT_SUBJECT
);
// send the email here using the impersonated $client
}
}
This works fine, however I'm running into an issue when I want to send a mail from a mail alias of that user. In that case, the from address doesn't match the google account to impersonate and sending the mail doesn't work. I'm looking for a way to pass the google account to impersonate to the Mailtransport. As an instance of this class is automatically created by the framework and only Swift_Mime_SimpleMessage can be used, my best solution right now is to add a custom header to the email which is read and then removed by the MailTransport class. This feels like a dirty workaround however, does anybody know a cleaner solution?