tags:

views:

273

answers:

3

hai

I have a Spanish site in php. In this a mail body contain a subject "Solicitud de cotización" but the subject appears in hot mailbox like Solicitud de cotización‏. But it's appear correct in mail section. How I avoid this problem. Does any one know this?

+1  A: 

You need to encode the subject header in whatever text encoding you are using. See http://search.cpan.org/~dankogai/Encode-2.39/lib/Encode/MIME/Header.pm and http://www.faqs.org/rfcs/rfc2047.html which talk about doing so.

Essentially, your subject line should look like this:

Subject: Solicitud de =?UTF-8?Q?cotizaci=C3=B3n?=

Then any MUA that knows about MIME should render the subject correctly, using the correct character set.

EDIT: It is worth mentioning that RFC2822 specifies ASCII as the character encoding for mail message headers, which is why the quoting is necessary. Also it specifies that lines should not be longer than 72 characters, so folding may be necessary and you should take that into consideration when generating messages destined for processing in RFC(2)822 mail systems. Finally, use of the B encoding does not make a great deal of sense for a string like you have supplied, since the Q encoding takes up less space (and for that matter, you only want to quote a run of words that actually use characters outside of the ASCII character set).

It is technically possible to just brute-force with B encoding for the whole string, but it's generally speaking bad form due to being wasteful, and it is much more likely that you will exceed the hard limit of 9,999 characters in a single line specified by the RFC(2)822 standards that way if ever you have a long subject line.

Michael Trausch
-1, Not a real answer... Provides no solution whatsoever.
Alix Axel
I'm sorry, the guy wanted to know how to make it work. Linking to the specification and providing an answer that explains why that specification is useful (assuming the OP is a programmer, of course).I don't have the time to spoon-feed a class, but I think it is less useful to give a solution to someone asking a question that forsakes the letter of the specifications that we have which hold the Internet together. If you don't care about RFC compliance, at least make that clear.
Michael Trausch
@Michael: From my experience @ SO, links are not generally considered as useful answers.
Alix Axel
If I weren't under NDA, I'd post a working class for mail generation compliant with RFC2822 and the MIME RFCs—albeit in a different language. Since I don't have the time to provide code that is correct, but I do have the time to provide a pointer (including code in Perl, which can be adapted) so that the OP can write correct code, I elected to provide the information that the OP can use to write correct code. I take pride in my work, as I should think all of us would; if that is the case, it is better to provide correct info than it is to provide something that won't comply in most situations.
Michael Trausch
A: 

Try setting the charset to utf-8 in the email headers:

charset=UTF-8

Or see:

UTF8 ready php mail function

UTF8_mail(
“fromname <[email protected]>”,
“First Last <[email protected]>”,
“Solicitud de cotización”,
“Κείμενο Text”,
“”,
“Solicitud de cotización<[email protected]>”
);
Sarfraz
As I understand MIME, that will take effect for the body. The headers still need to be encoded per RFC 2047.
Michael Trausch
@Michael Trausch: The link that I have provided discusses that too other than `charset`.
Sarfraz
Whoa. And this UTF8_mail function also suffers the same problem that the solution from Alix Axel supplied, that is, it encodes the string wastefully using the B encoding (Q is much more efficient in this case—and most other cases, too, actually) and does not fold the header if it reaches beyond 72 characters.
Michael Trausch
A: 

Hai! =) To encode the subject you have to do this:

$subject = 'Solicitud de cotización';

// =?UTF-8?B?U29saWNpdHVkIGRlIGNvdGl6YWNpw7Nu?=
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';

If you're using PHP 5.3+ you can use quoted_printable_encode() instead:

$subject = 'Solicitud de cotización';

// =?UTF-8?Q?Solicitud de cotizaci=C3=B3n?=
$subject = '=?UTF-8?Q?' . quoted_printable_encode($subject) . '?=';
Alix Axel
There is one major problem with that solution: It does not fold at 72 characters, which is how programs creating mail in compliance with RFC2822's full specification must behave. It also uses a lot of space, since it will grow the string unnecessarily by quoting characters that need no encoding at all, since they are in the ASCII character set of RFC2822 mail.
Michael Trausch
@Michael: Funny how you seem to be downvoting everyone and not be downvoted since you're the only one who hasn't provided a specific solution (I can link to RFC too you know?)...
Alix Axel
I don't have the time right this minute to whip up a class for processing mail to the letter of the standard; however, I have read the standards and they're not terribly difficult to read. I am making the assumption that the OP is able to write code to handle the situation given the pointers to the knowledge that is available for interoperating with the mail systems that adhere to RFCs.I downvoted for no other reason than the recommendations which clearly go against what the RFC's requirements and recommendations are. No personal offense is intended.
Michael Trausch
Alix Axel
I do not vote up or down relative to other answers. If an answer isn't correct, I downvote. Remember, downvoting costs *me* reputation as well. I don't have incentive to downvote just for the hell of it, nor does anyone else. I did provide a summary of what is needed, and there still isn't a full-code answer here. I don't expect people to spoon-feed me code, though maybe I am in the minority. I won't give code that I wouldn't run in production myself, I'll downvote suggestions and code which are bad examples and doesn't even bother to mention corner cases. It's just how I work.
Michael Trausch
For that matter, the solutions are faster to implement and execute, but that does __not__ mean that they are _correct_.
Michael Trausch
@Michael: I don't give a rat's ass about rep. You link to CPAN on a PHP tagged question and a RFC with almost five thousand words provide information indeed, but require time to read, understand and implement. My answer provides a working solution that **is correct** (as in **works**) pretty much on all situations. If you don't have the time to provide any real solution of your own fine... Just don't downvote other rather useful answers.
Alix Axel
We must be very different types of programmers. I subscribe to the philosophy of getting things right the first time, which usually means spending more time up-front to truly understand the ins and outs of what is being written, doing the research to implement it correctly, and then going from there to do so such that I don't have to revisit the topic. When writing networked programs, that means reading RFCs. In any case, I see no point for further discussion with you here, since we clearly do not agree on what's useful.
Michael Trausch