Skip to content

Instantly share code, notes, and snippets.

@M-Abdullahi
Created May 11, 2019 12:26
Show Gist options
  • Save M-Abdullahi/8fabef67d9fa2bd73598e1db626b4450 to your computer and use it in GitHub Desktop.
Save M-Abdullahi/8fabef67d9fa2bd73598e1db626b4450 to your computer and use it in GitHub Desktop.
$transport = new Swift_SmtpTransport('SMTP-HOST-HERE', SMTP-PORT-HERE);
$transport->setUsername('SMTP-USERNAME-HERE');
$transport->setPassword('SMTP-PASSWORD-HERE');
// extra configurations here if needed
Then, we use the Swift_SmtpTransport to build our Swift_Mailer object.
$swift_mailer = new Swift_Mailer($transport);
$view = app()->get('view');
$events = app()->get('events');
$mailer = new Mailer($view, $swift_mailer, $events);
$mailer->alwaysFrom('from-email', 'from-name');
$mailer->alwaysReplyTo('from-email', 'from-name');
$mailer->to('recipient')->send(new SomeMailable());
// AppServiceProvider.class
public function register()
{
$this->app->bind('user.mailer', function ($app, $parameters) {
$smtp_host = array_get($parameters, 'smtp_host');
$smtp_port = array_get($parameters, 'smtp_port');
$smtp_username = array_get($parameters, 'smtp_username');
$smtp_password = array_get($parameters, 'smtp_password');
$smtp_encryption = array_get($parameters, 'smtp_encryption');
$from_email = array_get($parameters, 'from_email');
$from_name = array_get($parameters, 'from_name');
$from_email = $parameters['from_email'];
$from_name = $parameters['from_name'];
$transport = new Swift_SmtpTransport($smtp_host, $smtp_port);
$transport->setUsername($smtp_username);
$transport->setPassword($smtp_password);
$transport->setEncryption($smtp_encryption);
$swift_mailer = new Swift_Mailer($transport);
$mailer = new Mailer($app->get('view'), $swift_mailer, $app->get('events'));
$mailer->alwaysFrom($from_email, $from_name);
$mailer->alwaysReplyTo($from_email, $from_name);
return $mailer;
});
$configuration = [
'smtp_host' => 'SMTP-HOST-HERE',
'smtp_port' => 'SMTP-PORT-HERE',
'smtp_username' => 'SMTP-USERNAME-HERE',
'smtp_password' => 'SMTP-PASSWORD-HERE',
'smtp_encryption' => 'SMTP-ENCRYPTION-HERE',
'from_email' => 'FROM-EMAIL-HERE',
'from_name' => 'FROM-NAME-HERE',
];
$mailer = app()->makeWith('user.mailer', $configuration);
// Use $mailer here...
class UserMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $configuration;
public $to;
public $mailable;
/**
* Create a new job instance.
*
* @param array $configuration
* @param string $to
* @param Mailable $mailable
*/
public function __construct(array $configuration, string $to, Mailable $mailable)
{
$this->configuration = $configuration;
$this->to = $to;
$this->mailable = $mailable;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$mailer = app()->makeWith('user.mailer', $this->configuration);
$mailer->to($this->to)->send($this->mailable);
}
}
$configuration = [
'smtp_host' => 'SMTP-HOST-HERE',
'smtp_port' => 'SMTP-PORT-HERE',
'smtp_username' => 'SMTP-USERNAME-HERE',
'smtp_password' => 'SMTP-PASSWORD-HERE',
'smtp_encryption' => 'SMTP-ENCRYPTION-HERE',
'from_email' => 'FROM-EMAIL-HERE',
'from_name' => 'FROM-NAME-HERE',
];
UserMailerJob::dispatch($configuration, 'recipient', new SomeMailable());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment