Created
January 12, 2024 16:04
-
-
Save saaiful/47232554123385c12569347d8480aad4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => '[email protected]', | |
'password' => 'secret', | |
]; | |
sendEmailWithCustomSMTP('[email protected]', \App\Mail\MyCustomMail::class, $smtpConfig); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment