53

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@address.com

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.");
cezar
  • 11,616
  • 6
  • 48
  • 84
  • `$email` and `$subject` are out of scope aren't they? – Anthony Sterling Feb 03 '14 at 15:04
  • I have defined them prior to the Mail::send call, but if it means out of scope, then what should I do? –  Feb 03 '14 at 15:11
  • 5
    Use `use`. `Mail::send('emails.activation', $data, function($message) use ($email, $subject) { ...`. :) – Anthony Sterling Feb 03 '14 at 15:24
  • would you please answer it with a rather complete code? and in case of success I hit it as answer –  Feb 03 '14 at 15:40
  • Also there is same issue will be fired when some times wrong email id will be there `(i.e. no where available abcd@abcd.abc)`. – bharat Feb 05 '18 at 06:33
  • i noticed that when i pass variable to `to()` than this error produce but when i manually write to this `to('example@gmail.com)` than it works. Its strange :( – TarangP Oct 31 '20 at 06:13
  • Maybe you should make it a string `$email='email@address.com'` – kelrob-dev Jun 25 '21 at 14:23

18 Answers18

34

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);
});
10

(I'm using SwiftMailer in PHP)

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

$email = "someone@somewhere.com <Some One>";

When what I meant to be sending was

$email = Array("someone@somewhere.com"=>"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.

Ghost8472
  • 191
  • 2
  • 9
7

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

print_r($variable_passed);
Leonardo Raele
  • 2,400
  • 2
  • 28
  • 32
6
 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.

Jonathan Machado
  • 522
  • 4
  • 14
4

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
Lakhwinder Singh
  • 5,536
  • 5
  • 27
  • 52
4

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);
});
Jithesh Jose
  • 1,781
  • 1
  • 6
  • 17
  • 2
    Please add some explanation to your answer such that others can learn from it - **what** have you changed, and why does that make a difference? – Nico Haase May 09 '20 at 19:51
1

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.");
WebHQ
  • 711
  • 8
  • 21
1

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 :)

MD. Atiqur Rahman
  • 2,063
  • 4
  • 17
  • 30
1

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

$email = Array("Zaffar Saffee" => "myemail@gmail.com");
        $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('mymail@gmail.com'); // string, we need an array
Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77
1

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.

1

This happens when recipient email is empty.

Try to print $email to check if it has value.

Prem Sagar
  • 147
  • 2
  • 8
0

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

fsalazar_sch
  • 348
  • 2
  • 6
  • 17
0

Try this.

Mail::send('emails.activation', $data, function ($message) use ($email, $subject) {
    $message->to($email)->subject($subject);
})->with('title', "Registered Successfully.");
linktoahref
  • 7,812
  • 3
  • 29
  • 51
KTAnj
  • 1,346
  • 14
  • 36
0

[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

Matthew Farrell
  • 181
  • 2
  • 3
0

For Laravel < v7 : from email address can contain a space at the end or the begining

For Laravel >= 7 : the from email address has to be RFC compliant i.e it should not have any spaces at the end or begining of the mail address

Just to be on safer side, see if you can trim the email address before actually calling the mailer class

0

My solution to this was to switch my QUEUE CONNECTION from Database to Redis.

Misterwyz
  • 101
  • 1
  • 2
  • 6
0

I'll explain it here for the beginners. Any variable declared outside of a callback function, are out of scope for that callback function. so if you want to use such a variable inside a callback function, you'll have to put them inside use(). Like in the question: $email and $subject must have been declared outside the callback, so use use() to use such variables. Also, it can be any number of variables.

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

For the RFC part, SwiftMailer strictly implements RFC standards as it should bcz mailing services such Google, Apple strictly monitor everything related to email & flag emails as spams for domains that do not follow these standards.

Raush
  • 121
  • 1
  • 2
-1

Check that the email address is correct and doesn't include any spaces.

lky
  • 1,081
  • 3
  • 15
  • 31