'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.");
Solution 1:[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);
});
Solution 2:[2]
(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.
Solution 3:[3]
Make sure your email address variable is not blank. Check using
print_r($variable_passed);
Solution 4:[4]
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.
Solution 5:[5]
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);
});
Solution 6:[6]
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
Solution 7:[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.");
Solution 8:[8]
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 :)
Solution 9:[9]
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
Solution 10:[10]
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.
Solution 11:[11]
These error happen when the $email variable is empty or sometimes when the mail doesn´t exists, try with an existing mail
Solution 12:[12]
Try this.
Mail::send('emails.activation', $data, function($message) use($email,$subject){
$message->to($email)->subject($subject);
});
->with('title', "Registered Successfully.");
Solution 13:[13]
[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.'
);
}
}
Solution 14:[14]
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
Solution 15:[15]
My solution to this was to switch my QUEUE CONNECTION from Database to Redis.
Solution 16:[16]
This happens when recipient email is empty.
Try to print $email to check if it has value.
Solution 17:[17]
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.
Solution 18:[18]
Check that the email address is correct and doesn't include any spaces.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
