开发者

How do I make an arbitrary string email address safe?

I need to take an arbitrary string and make it safe to use as part of an email address, replacing characters if necessary. This is to submit text to an API that requires values to be encoded into the address the email is being sent to. However, this is fraught with pitfalls and I'd like to make sure that whatever is input isn't going to break mail delivery. I'm already planning on just double-quoting the entire local part, which is going to mitigate a lot of the issues.

I've found a guide to what characters can be used in the local part of an email address, but it seems to make no distinction between 'It's forbidden by the RFCs' and 'It can be confusing so it's best to stay away' and 'it can only be used when escape开发者_JAVA技巧d properly'. Does anyone have a reference to something clearer/faster to read than the appropriate RFCs themselves?

Edit: I have no control over the parsing on the receiving end nor can I change how the text gets submitted as something other than a straight ASCII string.


The RFCs themselves are perfectly clear. Ignoring obsolete and quoted forms, the local part of an address is:

atext       =       ALPHA / DIGIT / ; Any character except controls,
                    "!" / "#" /     ;  SP, and specials.
                    "$" / "%" /     ;  Used for atoms
                    "&" / "'" /
                    "*" / "+" /
                    "-" / "/" /
                    "=" / "?" /
                    "^" / "_" /
                    "`" / "{" /
                    "|" / "}" /
                    "~"


Assuming you control the API, why not Base64 encode the data?

Base64EncodeString("Hello")
Base64DecodeString("SGVsbG8=")

You should probably replace the padding character = with an email-safe character like - minus.

Edit
It seems = is a safe character, no need to replace it.
However, the resulting text may start with a number so pad it with a letter and have the receiver discard the padding.


Why don't you just confine the characters you use in the email address to the ones in the table you referenced that are marked "OK"?

That would include plus, minus, hyphen, period, upper and lower case letters, numeric digits, and the underscore.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜