I use sendmail in Perl to send a result to myself, using the verified username of the person that submitted in the FROM field.
Every once in awhile, though, I'll come across a user with something unexpected in their username. Like today, someone's username had a comma; eg, "foo,bar". Which is perfectly acceptable, but then it tried to email it from
foo@my_server_name.com,bar <myemail@mydomain.com>.
Which led to a bunch of bounced messages that multiple addresses aren't allowed in the FROM field.
I was about to just use a regex to convert commas to %2C, but I'm not sure if that's really the right solution. And I realize that I probably have to worry about other special characters, too. So maybe I should convert all non-alphanumeric characters to a -? Eg:
$str =~ s/[^\w-]/-/g;
$str =~ s/-+/-/g;
Is there a better way to RFC-something encode the username here without using a whole module for it?