【问题标题】:Address in mailbox given [] does not comply with RFC 2822, 3.6.2. when email is in a variable给定 [] 邮箱中的地址不符合 RFC 2822, 3.6.2。当电子邮件在变量中时
【发布时间】:2014-02-27 03:04:54
【问题描述】:

我有一个正确的电子邮件地址。我已经回显了它,但是当我发送它时,我收到以下错误:

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

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

$email = email@address.com

然后当我发送它时,就会抛出错误。

但如果我直接使用字符串,它会发送它。

这里是块:

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

【问题讨论】:

  • $email$subject 超出范围不是吗?
  • 我已经在 Mail::send 调用之前定义了它们,但是如果这意味着超出范围,那我该怎么办?
  • 使用useMail::send('emails.activation', $data, function($message) use ($email, $subject) { ...。 :)
  • 能否请您用一个相当完整的代码来回答?如果成功,我将其作为答案
  • 还有同样的问题,有时会出现错误的电子邮件 ID (i.e. no where available abcd@abcd.abc)

标签: php email laravel laravel-4 swiftmailer


【解决方案1】:

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

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

【讨论】:

  • use ($email, $subject)
【解决方案2】:

(我在 PHP 中使用 SwiftMailer)

当我不小心为 $email 发送了一个字符串时,我遇到了这样的错误

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

当我打算发送的是

$email = Array("someone@somewhere.com"=>"Some One");

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

【讨论】:

  • 谢谢,这是我的问题,因为电子邮件的字符串不正确。
  • symfony 3.4 的一些问题(也使用 swiftmailer)
  • 这是我正在寻找的答案,谢谢。 ?
【解决方案3】:

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

print_r($variable_passed);

【讨论】:

  • 他/她在问题描述中说他们回显了变量并且它不是空白的。
  • 就我而言,这就是问题所在:空白邮件地址。
【解决方案4】:
 Mail::send('emails.activation', $data, function($message){
        $message->from('email@from', 'name');
        $message->to($email)->subject($subject);
    });

我不知道为什么,但就我而言,我将 from 的信息放入函数中,它工作正常。

【讨论】:

    【解决方案5】:

    对我有用的唯一解决方案是更改以下代码

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

    【讨论】:

    • 请在您的答案中添加一些解释,以便其他人可以从中学习 - 什么您改变了,为什么会有所不同?
    【解决方案6】:

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

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

    dd(env('MAIL_USERNAME') 
    

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

    php artisan cache:clear
    

    【讨论】:

      【解决方案7】:

      数据变量($email、$subject)似乎是全局的。并且无法在函数内部读取全局变量。您必须将它们作为参数传递(推荐的方式)或将它们声明为全局。

      试试这个方法:

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

      【讨论】:

        【解决方案8】:

        我遇到了同样的问题,我已经解决了。请确保以下内容:

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

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

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

        谢谢你:)

        【讨论】:

          【解决方案9】:

          我今天遇到了非常相似的问题,解决方法就是这样..

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

          虽然它是 laravel 5.2...

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

          ->emailWrittenOutputTo('mymail@gmail.com'); // string, we need an array
          

          【讨论】:

            【解决方案10】:

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

            【讨论】:

              【解决方案11】:

              当 $email 变量为空或邮件不存在时会发生这些错误,请尝试使用现有邮件

              【讨论】:

                【解决方案12】:

                试试这个。

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

                【讨论】:

                  【解决方案13】:

                  [已解决] 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

                  【讨论】:

                    【解决方案14】:

                    对于 Laravel

                    对于 Laravel >= 7:发件人电子邮件地址必须符合 RFC,即它不应在邮件地址的末尾或开头有任何空格

                    为了安全起见,请在实际调用邮件程序类之前查看是否可以修剪电子邮件地址

                    【讨论】:

                      【解决方案15】:

                      我对此的解决方案是将我的 QUEUE CONNECTION 从数据库切换到 Redis。

                      【讨论】:

                        【解决方案16】:

                        当收件人电子邮件为空时会发生这种情况。

                        尝试打印$email以检查它是否有价值。

                        【讨论】:

                          【解决方案17】:

                          我会在这里为初学者解释一下。 在回调函数之外声明的任何变量都超出了该回调函数的范围。所以如果你想在回调函数中使用这样的变量,你必须把它们放在 use() 中。 就像问题一样: $email 和 $subject 必须在回调之外声明,所以使用 use() 来使用这些变量。此外,它可以是任意数量的变量。

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

                          对于 RFC 部分,SwiftMailer 严格执行 RFC 标准,因为它应该 bcz 邮件服务,如 Google、Apple 严格监控与电子邮件相关的所有内容并将电子邮件标记为不符合这些标准的域的垃圾邮件。

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 2020-07-01
                            • 2012-12-28
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2022-07-03
                            • 2019-07-11
                            相关资源
                            最近更新 更多