Left Icon Right Icon
Squehub SqueHub

✉️ Mail Sending in Squehub

The Mail class in app/core/mail.php provides a powerful, flexible wrapper around PHPMailer. It supports SMTP configuration, templated HTML emails with Blade-like syntax, layouts, and inline view data injection.

📦 Namespace

To use Mail, include this namespace at the top of your file:

use App\Core\Mail;

🛠 SMTP Configuration (config/mail.php)

Here is a sample SMTP config file you should create at config/mail.php:

<?php
// config/mail.php
return [
    'host' => 'mail.Squehub.com',
    'port' => '587',
    'username' => 'dev@Squehub.com',
    'password' => '123456790$',
    'encryption' => 'tls',
    'from_address' => 'dev@Squehub.com',
    'from_name' => 'Squehub Framework'
];

🔧 Key Methods

  • to($address, $name = '') – Set recipient email and optional name.
  • subject($subject) – Set the email subject line.
  • body($htmlContent) – Set raw HTML content as email body.
  • replyTo($address, $name = '') – Add reply-to email address.
  • layout($layout) – Specify an email layout template (optional).
  • viewPath($path) – Set a custom directory path for email templates.
  • template($view, array $data = []) – Load and parse an email view template with data, applying layout if set.
  • send() – Send the email, returns true on success or throws on failure.

📋 Email Template Structure

Email templates use Blade-like syntax and reside by default in views/emails/. Each template has the .squehub.php extension.

You can optionally use layouts to wrap your email content with consistent headers/footers.

🛠️ Example Usage

use App\Core\Mail;

$mail => new Mail();

$mail->to('user@example.com', 'John Doe')
    ->subject('Welcome to Squehub!')
    ->template('welcome', ['name' => 'John'])
    ->layout('default')
    ->send();

⚙️ Custom View Path

If you want to organize your email templates in a custom directory (e.g., project/emails), set the path before calling template():

$mail->viewPath(__DIR__ . '/../../project/emails')
     ->template('notification', $data)
     ->send();

📑 Blade-like Template Syntax

Supported directives inside email templates include:

  • @extends('layout') – To extend a layout template.
  • @section('name') ... @endsection – Define sections to inject into layouts.
  • @yield('name') – Output section content inside layouts.
  • @include('partial') – Include partial templates.
  • Conditionals (escape all below):
    @if(condition)
    ... 
    @elseif(condition)
    ...
    @else
    ...
    @endif
    
  • Loops (escape all below):
    @foreach($items as $item)
    ...
    @endforeach
    
  • Variable echoing with @variable syntax (auto-escaped)

⚠️ Error Handling

If the email template or layout file is missing, or if sending fails, exceptions are thrown with descriptive messages. Make sure your configuration in config/mail.php is correct.

📘 Full Example with Template and Layout

<!-- views/emails/welcome.squehub.php -->
@extends('default')

@section('content')
    <h1>Hello, @name!</h1>
    <p>Thanks for joining Squehub.</p>
@endsection
<!-- views/emails/default.squehub.php -->
<html>
<body>
    <header><h2>Squehub Newsletter</h2></header>

    @yield('content')

    <footer><p>© 2025 Squehub</p></footer>
</body>
</html>

✅ Summary

  • Use Mail for robust SMTP email sending with templating.
  • Leverages PHPMailer and Blade-like syntax for reusable templates.
  • Supports layouts, sections, variables, conditionals, and loops in email views.
  • Allows custom view paths to organize email templates.
  • Throws exceptions on missing templates or send failures for easier debugging.
💡 Tip: Store your SMTP config securely in config/mail.php. Use email templates with layouts for consistent, branded messaging.
;