I'm trying to send mail using SwiftMailer. My problem is the format in which I'm getting the from
and to
address is like this:
$from = '"first last" <email@domain.com>';
But when I use this
$message = Swift_Message::newInstance()->setFrom($from);
I get the following error:
Address in mailbox given ["first last" <email@domain.com>] does not comply with RFC 2822, 3.6.2
I think it is because Swift mailer excepts the format to be
$message = Swift_Message::newInstance()->setFrom(['email@domain.com' => 'First Last']);
So my question is how do I convert
"first last" <email@domain.com> => ['email@domain.com' => 'First Last']
I can use regex but there can be many cases like if quotes are not present, if email is with and without angular brackets, etc?
What is the best most foolproof way to do this?
Edit: Adding some more info for clarification
The from
and to
email are what the users are saving in the database, they are coming in many formats like "name" <email>
, name <email>
, <email>
, or sometimes just email
. I want to normalize all these string inputs to something that I can pass to Swift_Message::newInstance()->setFrom
function