php 给定的邮箱地址 [] 不符合 RFC 2822, 3.6.2。当电子邮件在变量中时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21530566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:14:14  来源:igfitidea点击:

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

phpemaillaravellaravel-4swiftmailer

提问by

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:

为什么?我使用 laravel (swift mailer) 发送电子邮件:

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

回答by Juanjo Lainez Reche

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

由于范围的原因,您的电子邮件变量为空,您应该设置一个 use 子句,例如:

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

回答by Ghost8472

(I'm using SwiftMailer in PHP)

(我在 PHP 中使用 SwiftMailer)

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

当我不小心为 $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.

我不小心通过一个用于日志记录的 stringify 函数运行它,所以一旦我再次开始发送数组,错误就消失了。

回答by Shawinder Jit Singh

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

确保您的电子邮件地址变量不为空。检查使用

print_r($variable_passed);

回答by Jonathan Machado

 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.

我不知道为什么,但在我的情况下,我将 from 的信息放在函数中并且它工作正常。

回答by MD. Atiqur Rahman

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

这应该是您的 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

您为什么不能将 $email 变量调用到邮件发送函数中的答案。您需要调用 $user 变量,然后您可以将所需的变量写为 $user->variable

Thank You :)

谢谢你 :)

回答by Bidhan Mondal

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.

这是因为正在发送的电子邮件地址是空白的。看到那些空括号了吗?这意味着电子邮件地址没有被放入 swiftmailer 函数的 $address 中。

回答by Jithesh Jose

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

回答by Matthew Farrell

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

[已解决] Neos/swiftmailer:给定的邮箱地址 [] 不符合 RFC 2822, 3.6.2

/var/www/html/vendor/Packages/Libraries/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php 第 261 行中的异常:给定邮箱中的地址 [] 不符合 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

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

回答by Jeff Zeller

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

您的问题可能是 .env 文件未正确加载并使用 MAIL_USERNAME。

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

要检查您的 .env 文件是否正确加载了电子邮件地址,请将这一行添加到您的控制器并刷新页面。

dd(env('MAIL_USERNAME') 

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

如果它显示为空,请尝试从命令行运行以下命令并重试。

php artisan cache:clear

回答by Zaffar Saffee

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

虽然它是laravel 5.2...

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

我的基本问题是,我传递的是字符串而不是数组,所以错误是

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