xxxxxxxxxx
use Illuminate\Support\Facades\Mail;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Log;
// Replace the placeholders with your SMTP server credentials
$smtpConfig = [
'driver' => 'smtp',
'host' => 'your_smtp_host',
'port' => 'your_smtp_port',
'encryption' => 'tls', // adjust according to your SMTP server setup
'username' => 'your_smtp_username',
'password' => 'your_smtp_password',
'timeout' => '30',
'auth_mode' => null, // adjust according to your SMTP server setup
];
// Configure Laravel to use the SMTP server for sending emails
config(['mail.mailers.smtp' => $smtpConfig]);
// Create and send the email using the configured SMTP server
try {
Mail::mailer('smtp')->send([], [], function (Message $message) {
$message->to('recipient@example.com');
$message->subject('Example Email');
$message->setBody('This is the content of the email.', 'text/html');
// Add any additional settings or attachments here
});
// Email sent successfully
Log::info('Email sent successfully.');
} catch (\Exception $e) {
// Handle any errors that occurred during email sending
Log::error('Failed to send email: ' . $e->getMessage());
}
xxxxxxxxxx
public function index()
{
$data = array('name'=>"vikash Mirdha");
Mail::send('mailview',$data,function($message) {
$message->to('jobs9493@gmail.com');
$message->subject('Welcome Mail');
});
dd('Mail Send Successfully');
xxxxxxxxxx
use Illuminate\Support\Facades\Mail;
use App\Mail\DemoEmail;
public function sendEmail()
{
$email = 'recipient@example.com';
$data = [
'name' => 'John Doe',
'message' => 'This is a test email from Laravel with Mailtrap.'
];
Mail::to($email)->send(new DemoEmail($data));
return "Email sent successfully.";
}
xxxxxxxxxx
Mail::send(['html' => 'admin.email-template.lowstock'], array('totalUsers' => $totalUsers, 'item' => $item,'data' =>$data), function ($message) use ($user_to_mail, $item) {
$message->from('POS@gmail.com', 'POS');
$message->subject('Notification Mail');
foreach ($user_to_mail as $mail) {
$message->to($mail->email);
}
});
}
This works for Laravel 9, and hopefully above.
https://blog.genijaho.dev/allowing-users-to-send-emails-with-their-own-smtp-settings-in-laravel-9
xxxxxxxxxx
// 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;
});
}