Address in mailbox given [] does not comply with RFC 2822, 3.6.2. when email is in a variable

Address in mailbox given [] does not comply with RFC 2822, 3.6.2. when email is in a variable

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Address in mailbox given [] does not comply with RFC 2822, 3.6.2. when email is in a variable

You are encountering a classic PHP variable interpolation failure within your Laravel mailer. The error message Address in mailbox given [] does not comply with RFC 2822, 3.6.2 indicates that the Swift Mailer driver received an empty string as the recipient address, rather than the valid email you intended to send. This happens because the variable holding the email address is effectively empty at the moment the mailer executes.

The root cause lies in how PHP handles variables inside Mail::send(). When you define $email outside the closure and pass it into the to() method, you are relying on the scope resolution to carry that value forward. However, if $email is empty or undefined when the closure is executed, the mailer passes an empty string to the underlying SMTP transport. RFC 2822 strictly mandates that the "From" and "To" headers must contain a valid mailbox format; an empty string fails this check immediately, triggering the exception before any network request occurs.

The fact that you can send emails using a hardcoded string like email@address.com confirms that your SMTP configuration and driver settings are correct. The issue is isolated entirely to the data flow into the closure. In your specific code block, if $email contains whitespace or is an empty string when the view rendering begins, the to() method receives nothing.

To resolve this, you must ensure the variable is populated before it reaches the mailer. A robust pattern involves validating the input at the point of usage or ensuring the variable exists in the closure's scope with a default value if necessary. Instead of relying on external state that might be null, assign the address directly within the closure or pass a validated array from your controller:

// Controller action
$data['email'] = request('email') ?: 'default@example.com'; // Fallback strategy

Mail::send('emails.activation', $data, function($message) use ($email) {
    $message->to($email)->subject($subject);
});

Using the use keyword explicitly binds the outer scope variable into the closure. This guarantees that $email is evaluated in the current context. If you are passing $data to the view and expecting the email to be inside an array, ensure that the key exists and holds a non-empty string. Empty arrays or missing keys often default to null, which PHP treats as an empty value in string contexts.

Another common pitfall involves whitespace. If your database stores email@address.com with a trailing space, standard string comparison might pass, but Swift Mailer's internal validation is stricter. It trims strings before sending, but if the logic depends on specific formatting, you should explicitly trim the variable:

$email = trim($data['email']);

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Proceed with sending
} else {
    // Handle validation error
}

The filter_var function provides a reliable way to enforce RFC standards before passing the value to Swift Mailer. This prevents the exception from occurring deep within the mailer stack and allows you to handle invalid addresses gracefully in your application logic.

If you are integrating with a service like LaravelCompany, their documentation often highlights best practices for handling dynamic payloads in email templates. They emphasize that variables passed to views should be sanitized and validated before being used in critical operations like sending notifications. Following these guidelines ensures that your mail queue does not accumulate failed jobs due to malformed headers.

Ultimately, the error is a symptom of an empty or invalid variable reaching the mailer's to() method. By enforcing validation and ensuring proper scope resolution within your closures, you eliminate the discrepancy between echoing the value in development and failing during production sends.

Our network