Skip to content

Instantly share code, notes, and snippets.

@saaiful
Created January 12, 2024 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save saaiful/47232554123385c12569347d8480aad4 to your computer and use it in GitHub Desktop.
Save saaiful/47232554123385c12569347d8480aad4 to your computer and use it in GitHub Desktop.
use Illuminate\Support\Facades\Mail;
use Swift_SmtpTransport;
function sendEmailWithCustomSMTP($to, $emailClass, $smtpConfig)
{
// Create the transport
$transport = new Swift_SmtpTransport($smtpConfig['host'], $smtpConfig['port']);
$transport->setUsername($smtpConfig['username']);
$transport->setPassword($smtpConfig['password']);
$transport->setEncryption($smtpConfig['encryption']);
// Create the Mailer using your created Transport
$mailer = new \Swift_Mailer($transport);
// Set Laravel's Mailer
Mail::setSwiftMailer($mailer);
// Now send the email
Mail::to($to)->send(new $emailClass());
// Optionally, reset to the default mailer after sending
// Mail::resetSwiftMailer();
}
// Usage
$smtpConfig = [
'host' => 'smtp.example.com',
'port' => 587,
'encryption' => 'tls',
'username' => 'username@example.com',
'password' => 'secret',
];
sendEmailWithCustomSMTP('recipient@example.com', \App\Mail\MyCustomMail::class, $smtpConfig);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment