I am having a really strange error in my email code. I am pulling a list of all Admin users from my user DB which is 4 right now, I am generating a list of emails for the Admin. I then try to send an email and have it go to all Admin users (4 in my case).
Now after some formating my list of 4 emails is stored in a variable named $toEmail which is a comma seperated lost and each email wrapped in quotes so the result of that variable looks like this...
'one@email.com', 'two@email.com', 'three@email.com', 'four@email.com'
So when I send my email and setup the to and cc fields,
$message->to('one@email.com')->cc($toEmails);
I get this awful error message...
Swift_RfcComplianceException
Address in mailbox given ['one@email.com', 'two@email.com',
'three@email.com', 'four@email.com']
does not comply with RFC 2822, 3.6.2.
Now what is really crazy is if I replace the variable above in my cc() code with a text string of... 'one@email.com', 'two@email.com', 'three@email.com', 'four@email.com'
Then the email works just fine with no errors! Switch it out again with the variable $toEmails and I get that awful message again! Craziness right?
Now if I print out the $toEmails variable to screen, it shows the exact value that I had put in as a text atring, so why would it not work as a variable but will work as a text string makes no sense to me?
All my searches for a solution are showing people that have the same error except that there email list is blank due to them not passing in the email to the mail function and not making it global either. That is not my problem, my error message even shows the list of email addresses! Also I am getting my list from within the mail function itself so no issue there either.
That exception explained: Swiftmailer GitHub Issue
The way you have phrased your question setup is a little awkward.
When it errors, is your code like this:
$toEmails = " 'one@email.com', 'two@email.com', 'three@email.com', 'four@email.com' ";
$message->to('one@email.com')->cc($toEmails);
or like this:
$toEmails = array('one@email.com', 'two@email.com', 'three@email.com', 'four@email.com');
$message->to('one@email.com')->cc($toEmails);
or something else?
The RFC (specifically section 3.4) describes the address list construction:
valid address lists look like:
username@domain.tld
<username@domain.tld>
"User Name" <username@domain.tld>
username1@domain.tld, username2@domain.tld, username3@domain.tld
<username1@domain.tld>, <username2@domain.tld>, <username3@domain.tld>
"User Name 1" <username1@domain.tld>, "User Name 2" <username2@domain.tld>, "User Name 3" <username3@domain.tld>
I suspect that your single quotes simply don't follow the spec. try removing the single quotes from around the email addresses.
I've tested it using the following code:
$emails = ['myoneemail@esomething.com', 'myother@esomething.com','myother2@esomething.com'];
Mail::send('emails.welcome', [], function($message) use ($emails)
{
$message->to($emails)->subject('This is test e-mail');
});
var_dump( Mail:: failures());
exit;
Result - empty array for failures.
But of course you need to configure your app/config/mail.php properly. So first make sure you can send e-mail just to one user and then test your code with many users.
Moreover using this simple code none of my e-mails were delivered to free mail accounts, I got only emails to inboxes that I have on my paid hosting accounts, so probably they were caught by some filters (it's maybe simple topic/content issue but I mentioned it just in case you haven't received some of e-mails) .
Hello, I am having a trouble when sending my form that allows to offer the user a page that allows them to enter their email, that of an acquaintance and an accompanying message.
I have this error appears: Address in mailbox given [email] does not comply with RFC 2822, 3.6.2. related to control by the function assertValidAddress($address) present in the file MailBoxHeader.
Here is my code in the controller : [code]public function postShare() {
$rules = [
'email' => 'required|email', 'emailcollaborator' => 'required|email', 'message' => 'required|min:5',
];
$fields = Input::all(); $validator = Validator::make($fields, $rules);
$email = 'email'; // The form validation was good if ($validator->passes()) {
// We finally send an email to confirm the account $data = array(
'email' => $fields['email'], 'emailcollaborator' => $fields['emailcollaborator'], 'message' => $fields['message']
);
Mail::queue('emails.share', $data, function($message) use ($email) { $message->from($email, 'Moi'); $message->to('emailcollaborator')->subject('Welcome!'); });
//addLogAuth::user()->id, 'Demande de contact effectué');
Session::flash('message', "Votre message a bien été envoyé à votre destinataire");
return Redirect::to('/home/share');
} else {
// We return the same page with the error and saving the input datas return Redirect::back() ->withInput() ->withErrors($validator);
}
} [/code]
Can anyone give me a track to solve the difficulty I encounter?
plz make sure there is no white space before or after the email address given
I think you should try this
$emails[$i] = str_replace(' ', '', $emails[$i]);
Removing white-space on every emails you split is very important.. this will avoid such errors.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community