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

I have a correct email address. I have echoed it, but when I send it, I get the following error:

 Address in mailbox given [] does not comply with RFC 2822, 3.6.2. 

Why? I use laravel (swift mailer) to send email:

$email = [email protected]

and then when I send it, the error is thrown.

But if I directly use the string, it sends it.

Here is the block:

 Mail::send('emails.activation', $data, function($message){
            $message->to($email)->subject($subject);
        });
                ->with('title', "Registered Successfully.");

This question is related to php email laravel laravel-4 swiftmailer

The answer is


I have faced the same problem and I have fixed. Please make sure some things as written bellow :

   Mail::send('emails.auth.activate', array('link'=> URL::route('account-activate', $code),'username'=>$user->username),function($message) use ($user) {
                        $message->to($user->email , $user->username)->subject('Active your account !');

                    });

This should be your emails.activation

    Hello {{ $username }} , <br> <br> <br>

    We have created your account ! Awesome ! Please activate by clicking the   following link <br> <br> <br>
   ----- <br>
      {{ $link }} <br> <br> <br>
       ---- 

The answer to your why you can't call $email variable into your mail sending function. You need to call $user variable then you can write your desired variable as $user->variable

Thank You :)


[SOLVED] Neos/swiftmailer: Address in mailbox given [] does not comply with RFC 2822, 3.6.2

Exception in line 261 of /var/www/html/vendor/Packages/Libraries/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php: Address in mailbox given [] does not comply with RFC 2822, 3.6.2.

private function _assertValidAddress($address)
{
    if (!preg_match('/^'.$this->getGrammar()->getDefinition('addr-spec').'$/D',
        $address)) {
        throw new Swift_RfcComplianceException(
            'Address in mailbox given ['.$address.
            '] does not comply with RFC 2822, 3.6.2.'
            );
    }
}

https://discuss.neos.io/t/solved-neos-swiftmailer-address-in-mailbox-given-does-not-comply-with-rfc-2822-3-6-2/3012


Your email variable is empty because of the scope, you should set a use clause such as:

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

I had very similar problem today and solution was as it is..

$email = Array("Zaffar Saffee" => "[email protected]");
        $schedule->command('cmd:mycmd')
                 ->everyMinute()
                 ->sendOutputTo("/home/forge/dev.mysite.com/storage/logs/cron.log")
                 ->emailWrittenOutputTo($email);

It laravel 5.2 though...

my basic problem was , I was passing string instead of array so, error was

->emailWrittenOutputTo('[email protected]'); // string, we need an array

Its because the email address which is being sent is blank. see those empty brackets? that means the email address is not being put in the $address of the swiftmailer function.


These error happen when the $email variable is empty or sometimes when the mail doesn´t exists, try with an existing mail


The only solution worked for me is changing the following code

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

Your problem may be that the .env file is not loading properly and using the MAIL_USERNAME.

To check if your .env file is loading the email address properly add this line to your controller and refresh the page.

dd(env('MAIL_USERNAME') 

If it shows up null try running the following command from command line and trying again.

php artisan cache:clear

Try this.

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

(I'm using SwiftMailer in PHP)

I was getting an error like that when I was accidentally sending a string for $email

$email = "[email protected] <Some One>";

When what I meant to be sending was

$email = Array("[email protected]"=>"Some One");

I was accidentally running it through a stringify function that I was using for logging, so once I started sending the array again, the error went away.


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

I dont know why, but in my case I put the from's information in the function and it's work fine.


Data variables ($email, $subject) seems to be global. And globals cannot be read inside functions. You must pass them as parameters (the recommended way) or declare them as global.

Try this way:

Mail::send('emails.activation', $data, function($message, $email, $subject){
        $message->to($email)->subject($subject);
    });
            ->with('title', "Registered Successfully.");

Make sure your email address variable is not blank. Check using

print_r($variable_passed);

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to email

Monitoring the Full Disclosure mailinglist require(vendor/autoload.php): failed to open stream Failed to authenticate on SMTP server error using gmail Expected response code 220 but got code "", with message "" in Laravel How to to send mail using gmail in Laravel? Laravel Mail::send() sending to multiple to or bcc addresses Getting "The remote certificate is invalid according to the validation procedure" when SMTP server has a valid certificate How to validate an e-mail address in swift? PHP mail function doesn't complete sending of e-mail How to validate email id in angularJs using ng-pattern

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to laravel-4

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration 'Malformed UTF-8 characters, possibly incorrectly encoded' in Laravel Can I do Model->where('id', ARRAY) multiple where conditions? how to fix stream_socket_enable_crypto(): SSL operation failed with code 1 Rollback one specific migration in Laravel How can I resolve "Your requirements could not be resolved to an installable set of packages" error? Define the selected option with the old input in Laravel / Blade Redirect to external URL with return in laravel laravel the requested url was not found on this server

Examples related to swiftmailer

Failed to authenticate on SMTP server error using gmail Expected response code 220 but got code "", with message "" in Laravel Address in mailbox given [] does not comply with RFC 2822, 3.6.2. when email is in a variable Swift_TransportException Connection could not be established with host smtp.gmail.com PHP Swift mailer: Failed to authenticate on SMTP using 2 possible authenticators