Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Dynamic Mailer Configuration in Laravel with Mail::build

    Dynamic Mailer Configuration in Laravel with Mail::build

    November 22, 2024

    Dynamic Mailer Configuration in Laravel with Mail::build

    Need to configure mailers dynamically? Laravel’s Mail::build method lets you create mailers on the fly! Let’s explore this flexible approach to email configuration.

    Basic Usage

    Here’s how to create a mailer dynamically:

    use IlluminateSupportFacadesMail;
    
    $mailer = Mail::build([
        'transport' => 'smtp',
        'host' => '127.0.0.1',
        'port' => 587,
        'encryption' => 'tls',
        'username' => 'usr',
        'password' => 'pwd',
        'timeout' => 5,
    ]);
    
    $mailer->send($mailable);
    

    Real-World Example

    Here’s how you might use it in a multi-tenant application:

    class TenantMailService
    {
        public function sendWithTenantConfig(
            Tenant $tenant, 
            Mailable $mailable
        ) {
            $mailerConfig = $tenant->email_settings;
    
            $mailer = Mail::build([
                'transport' => 'smtp',
                'host' => $mailerConfig->smtp_host,
                'port' => $mailerConfig->smtp_port,
                'encryption' => $mailerConfig->encryption,
                'username' => decrypt($mailerConfig->username),
                'password' => decrypt($mailerConfig->password),
                'from' => [
                    'address' => $tenant->email,
                    'name' => $tenant->company_name
                ]
            ]);
    
            try {
                $mailer->send($mailable);
                
                Log::info("Email sent for tenant: {$tenant->id}", [
                    'mailable' => get_class($mailable)
                ]);
                
            } catch (Exception $e) {
                Log::error("Failed to send email for tenant: {$tenant->id}", [
                    'error' => $e->getMessage()
                ]);
                
                throw $e;
            }
        }
    }
    
    // Usage
    class NewsletterController extends Controller
    {
        public function send(
            Tenant $tenant, 
            TenantMailService $mailService
        ) {
            $newsletter = new TenantNewsletter($tenant);
            
            $mailService->sendWithTenantConfig(
                $tenant, 
                $newsletter
            );
            
            return back()->with('success', 'Newsletter queued for sending');
        }
    }
    

    Dynamic mailer configuration is perfect for multi-tenant applications, custom email providers, or any scenario where mail settings need to be configured at runtime.


    The post Dynamic Mailer Configuration in Laravel with Mail::build appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleWhy You Should Speak At Events As An Early-Career Professional
    Next Article 5 Ways Drupal Starshot Will Supercharge Your Website in 2025

    Related Posts

    Machine Learning

    LLMs Struggle with Real Conversations: Microsoft and Salesforce Researchers Reveal a 39% Performance Drop in Multi-Turn Underspecified Tasks

    May 17, 2025
    Machine Learning

    This AI paper from DeepSeek-AI Explores How DeepSeek-V3 Delivers High-Performance Language Modeling by Minimizing Hardware Overhead and Maximizing Computational Efficiency

    May 17, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Australian Privacy Commissioner Labels Third-Party Suppliers as a Privacy ‘Weak Spot’

    Development

    Bluesky adds TikTok mode while Elon mulls reviving Vine in the latest banning drama

    News & Updates

    Bitter APT Targets Turkish Defense Sector with WmRAT and MiyaRAT Malware

    Development

    NVIDIA Open-Sources cuOpt: An AI-Powered Decision Optimization Engine–Unlocking Real-Time Optimization at an Unprecedented Scale

    Machine Learning

    Highlights

    Development

    Ethical Implications of AI – Innovation with Accountability

    January 28, 2025

    The Rise of Ethical AI: Opportunities and Ethical Concerns Regulatory Landscape: Progress and Gaps Best Practices for Ethical AI Adoption How Tx Can Help You Lead Ethically in AI Conclusion: Innovation with Accountability 73% of executives believe that AI ethics will become a critical part of their businesses within the next five years, yet only … Ethical Implications of AI – Innovation with Accountability
    The post Ethical Implications of AI – Innovation with Accountability first appeared on TestingXperts.

    Gemini breaks new ground: a faster model, longer context and AI agents

    May 14, 2024

    Technology that changed us: The 2000s, from iPhone to Twitter

    February 6, 2025

    Windows 11 KB5052094 toggles off OneDrive alerts in Explorer, direct download .msu

    February 26, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.