Zap
Documentation / Utilities

Email

Configure SMTP credentials in your environment, compose a message with Zap's Email utility, and handle delivery results without exposing mail-server details to users.

Configure SMTP

The Email utility reads SMTP settings from config/email.php, which maps these environment variables. Configure them in your private .env file:

MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=notifications@example.com
MAIL_PASSWORD=replace-with-a-secret
MAIL_ENCRYPTION=tls

Port 587 with tls and port 465 with ssl are common SMTP configurations; use the values required by your mail provider. The configured username is also used as the sender address. Keep credentials private and never commit real secrets.

Compose and send

Resolve Email through the application container, then chain its configuration methods. The body is sent as HTML. execute() returns a result whose status is success or error.

use Zap\Core\Utils\Email;

$result = app(Email::class)
 ->sender_name('Zap Notifications')
 ->addressee('user@example.com', 'Zap User')
 ->subject('Your account update')
 ->body('<p>Your profile was updated.</p>')
 ->execute();

if ($result['status'] === 'error') {
 // Log the failure server-side and return a generic message to the user.
}
Sending requires phpmailer/phpmailer, included in the framework's Composer dependencies. The mailer connects to the configured SMTP server with a 10-second timeout.

Set recipients and BCC

Set one recipient with addressee(email, name). Add blind-copy recipients with bcc(), which accepts one address or an array of addresses and recipient records.

$email = app(Email::class)
 ->sender_name('Support')
 ->addressee('customer@example.com', 'Customer')
 ->bcc([
 'audit@example.com',
 ['email' => 'team@example.com', 'name' => 'Support Team'],
 ])
 ->subject('Support request received')
 ->body('<p>We received your request.</p>');

$result = $email->execute();

Attach files

Attach an existing server-side file with attachment_from_path(). The method throws an InvalidArgumentException if the path does not exist. Attachment paths should come from trusted application data, not directly from a request.

$result = app(Email::class)
 ->sender_name('Reports')
 ->addressee('user@example.com')
 ->subject('Your report')
 ->body('<p>Your report is attached.</p>')
 ->attachment_from_path(
 BASE_PATH . '/storage/reports/monthly.pdf',
 'monthly-report.pdf'
 )
 ->execute();

For uploaded attachments, pass the PHP $_FILES array chunk to attachments(). The utility moves accepted uploaded files into a temporary location and removes those temporary files after sending.

$email->attachments($_FILES['attachments']);

Handle email safely

  • Validate request data and authorize the action before composing or sending mail.
  • Escape untrusted values before inserting them into the HTML message body.
  • Do not display SMTP error messages or credentials to the requester.
  • Use BCC for recipients who should not see one another's addresses.
  • Validate uploaded attachments and apply size and type restrictions before accepting them.
Next: show flash messages

Continue with the Flasher utility guide.

Flasher utility