0

I am having a super strange issue, I will explain in detail why it is strange after the facts...

The code...

Mail::send('emails.cron.loggedinusers', $data, function($message){

    // Get Admin user emails
    $adminUserEmails = DB::table('users')->where('user_role', 'admin')->lists('email');

    // Build nice comma separated list of Admin Emails addresses  
    // and also wrap each email in quotes
    $filter = function($adminUserEmails){ return "'$adminUserEmails'"; };
    $toEmails = array_map($filter, $adminUserEmails);
    $toEmails = implode(', ', array_values($toEmails));
    print_r($toEmails);

    // Send email to Admins Only
    $message->from('one@email.com', 'Jason');
    $message->to('one@email.com')->cc($toEmails);
    $message->subject('[TIMECLOCK ALERT] Users are logged into the Timeclock!');
});

When I run this code above which gets 4 Email address from the database and tries to email to them, I get this nasty error message...

Keep in mind I have changed the actual email values shown here to protect there identity but the REAL emails in the script are all legit emails...

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 if I replace the variable $toEmails with the actual string text... $message->to('one@email.com')->cc('one@email.com', 'two@email.com', 'three@email.com', 'four@email.com');

Then it sends the emails without an issue and no error messages!

This is strange and weird to me because I am literally printing the $toEmails variable to screen and copy/pasting it's output into the email code above and it mails just fine but as soon as I use the variable instead of the text string, I get that awful error message above. I cannot make any sense of why this would do this, please if you have any ideas please share with me???

JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • I think here is the same issue. http://stackoverflow.com/questions/6508033/swift-mailer-error-address-in-mailbox-given-does-not-comply-with-rfc – Zaw Myo Htet Jun 21 '14 at 03:38
  • @ZawMyoHtet I know at first glance they appear similar...the difference is there problem is they are not passing in the email address into the email function and not making it global either, my code has access to the emails and the error even prints out the email address in a list as well...it is a very strange issue how it works as a string but not as a variable – JasonDavis Jun 21 '14 at 03:43

1 Answers1

1

The error message you get is a little misleading here and perhaps worth a bug-report. RFC 2822, 3.6.2. is about originator fields while to and cc aren't such an originator field, from would be.

However the other part of the message is telling exactly what the problem is:

Address in mailbox given ['one@email.com', 'two@email.com', 'three@email.com', 'four@email.com'] does not comply ...

And that's correct. It's not a mailbox, it's a mailbox-list. Swift mailer expects a mailbox here, not a list of mailboxes. And that's what the message is telling you. You failed to provide a mailbox here. You perhaps thought you could provide a list of mailboxes here, but obviously that gave you the exception so this won't work.

And you already did find the solution, by not providing a list but by adding one mailbox after the other, one per parameter.

This is more a question here what the interface of the $message class expects than anything else. And as the misleading part of the error message was not noted by you, I guess you were not mislead by that part.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836