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

。_饼干妹妹 提交于 2019-11-27 03:52:29

问题


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.");

回答1:


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



回答2:


(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.




回答3:


make sure your email address variable is not blank. check using

print_r($variable_passed);




回答4:


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




回答5:


 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.




回答6:


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.




回答7:


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.");



回答8:


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




回答9:


Try this.

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



回答10:


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



回答11:


[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




回答12:


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



回答13:


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


来源:https://stackoverflow.com/questions/21530566/address-in-mailbox-given-does-not-comply-with-rfc-2822-3-6-2-when-email-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!