Forum Moderators: coopster & phranque

Message Too Old, No Replies

Encoding text for sendmail

         

csdude55

6:36 pm on Aug 15, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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?

robzilla

7:54 pm on Aug 15, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Wrap it in quotes?

phranque

9:08 pm on Aug 15, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



An addr-spec is a specific Internet identifier that contains a
locally interpreted string followed by the at-sign character ("@",
ASCII value 64) followed by an Internet domain. The locally
interpreted string is either a quoted-string or a dot-atom.

addr-spec = local-part "@" domain

local-part = dot-atom / quoted-string / obs-local-part
(my emphasis)

The local-part portion is a domain-dependent string. In addresses,
it is simply interpreted on the particular host as a name of a
particular mailbox.


source: RFC 5322
Internet Message Format
Addr-Spec Specification
https://www.rfc-editor.org/rfc/rfc5322#section-3.4.1

therefore as suggested by robzilla, if your domain allows mailboxes that contain commas, and the FROM field uses the comma as a field separator, you should enclose with quotes the "local-part" of email addresses containing a comma.