John Davidson

php - Pass variables to custom mail transport in laravel

0 comments
Message:


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?



My Answer:
$failedRecipients)
{
$googleClient = new GoogleClient();

// Get the 'from' address from the message
$from = $message->getFrom();

// Impersonate the user using the 'from' address
$googleClient->setSubject($from);

// Send the email using the gmail API
// Code to send email using gmail API

return $this->_sendPerformed($message);
}
}

In this code snippet, I'm using the 'from' address of the email message to impersonate the user when sending the email using the gmail API. This works fine for most cases, but there are scenarios where I need to pass additional variables to the custom mail transport in order to perform the impersonation correctly.

One way to pass additional variables to the custom mail transport is to add them as parameters to the send() method. For example:

public function send(Swift_Mime_SimpleMessage $message, $additionalVariables)
{
// Use the additional variables to perform the impersonation
}

Then, when calling the send() method, you can pass the additional variables as arguments:

$mailTransport->send($message, $additionalVariables);

This way, you can pass any additional variables required for the custom mail transport to perform its actions correctly.

Rate this post

3 of 5 based on 5197 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us