UTF-8 sender name in Zend_Mail? - php

I am using Zend_Mail and want to customize the sender name.
I want the sender name to be FooBar爱你Ryan (where 'Ryan' gets replaced with the recipient name and 爱你 gets replaced with the translation for 'loves' in the language of the recipient, just like CD Baby does).
I've tried base64_encode and mb_encode_mimeheader() and other things like:
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
iconv_set_encoding("input_encoding", 'UTF-8');
iconv_set_encoding("output_encoding", 'UTF-8');
iconv_set_encoding("internal_encoding", 'UTF-8');
header('Content-Type:text/html; charset=' . 'UTF-8');
It generates this as the sender: '=?UTF-8?B?RXh0cmFidXjniLHkvaByY3dhbHNoQGV4dHJhYnV4LmNvbQ==?= <email#example.com>'
And then that appears in my Gmail as (unknown sender).
Any ideas?

For me the only solution worked was the following:
As you'd set utf8 subject in a usual php sendmail case, you can make a utf8 noted base64 string like this:
$mail->addFrom($fromEmail, '=?utf-8?B?'.base64_encode($fromName).'?=');
With this solution every thing worked like a charm.

I wish I had tried this earlier: when I hard code the Chinese string as the sender name (using utf8 characters), it works fine. (I tested in Gmail only.)
So the path I'd been going down was mistaken.
I need to figure out why a dynamically-generated sender name consisting of utf8 characters doesn't work when a hard-coded Chinese string does. But that seems to be a different question.

This is a great question and the answers are good - but ZendFramework advanced and the interfaces referenced became unfortunately obsolete.
So here is the same solution, but tested to work fine as of June/2017:
private static function ecvt($string)
{
return mb_convert_encoding($string, 'ISO-2022-JP', 'UTF-8');
}
private static function hcvt($string)
{
return "=?iso-2022-jp?B?" . base64_encode( self::ecvt($string) ) . "?=";
}
private function sendMail( )
{
$mail = new Message();
$content = 'Message body 日本語も';
$mail->getHeaders()->addHeaderLine('Content-Type', 'text/plain; charset=ISO-2022-JP');
$mail->setFrom('sender#acme.com', self::hcvt('Sender 日本語も') );
$mail->addTo('receiver#acme.com', self::hcvt('Receiver 日本語も') );
$mail->setSubject(self::hcvt('Some subject 日本語も'));
$mail->setBody( self::ecvt($content) );
$mail->setEncoding('ISO-2022-JP');
// this is critical - it works around a bug in zendframework3 where
// MIME encoding is botched in headers. By switching headers to ASCII,
// I basically do the encoding myself.
$mail->getHeaders()->setEncoding('ASCII');
$this->mailTransport->send( $mail );
}
The basis for all this really is here - it is good to read so you know what is going on: RFC2047 https://www.ietf.org/rfc/rfc2047.txt

Related

PHP imap how to decode email body correctly?

I've developed an email system on my own using php's imap and everything works fine, except for emails that are written in Arabic language, i've tried all the decoding functions and nothing seems to work. I got the subject to work perfectly by using imap_utf8 but not the email body.
This is what the email body looks like:
�
رحبا
هاي الرسالة �
This is my code:
$text = imap_fetchbody($imap, $uid, $partNumber, FT_UID);
switch ($structure->encoding) {
case 3:
return imap_base64($text);
case 4:
return imap_qprint($text);
default:
return $text;
}
If anyone can help with this issue. Thanks
I suggest you have a look at this: https://github.com/mantisbt-plugins/EmailReporting/blob/master/core/Mail/Parser.php
It uses the stuff you've used as well but it adds character encoding on top of it.
Subject character encoding can happen inline. For email bodies its one character set for the entire body
The script given uses a pear package, not the IMAP extension but based on your input it should be pretty equal
Hope this helps
Try to use quoted-printable-decode() function as stated here

phpmailer subject from variable creates encoding issue

I am using phpmailer to send emails using php.
When i try to send special (Turkish) character within the subject, it displays html entity in the sent email. If I include the same variable in the body part, it works fine. Please see below:
$mail->Subject = $stuname."PhD Qualifying Exam Application";
I have tried html_entity_decode function but didnt work.
Also, if I type the Turkish character instead of getting from a variable, it works fine.
Finally, if I print the variable before sending the email, it prints fine without any encoding problem. But number of character is larger than it should be..
So, any idea why I am having encoding problem in subject are when getting the value from a variable?
Thank you!
PS:
i am also adding these headers:
$mail->SetLanguage("tr", "phpmailer/language");
$mail->CharSet ="utf-8";
$mail->Encoding="base64";
I found a solution, none of the html decoding functions were working so I wrote my own function for Turkish characters.
function replacehtml($inputText) {
$replace = array('İ','ı','Ö','ö','Ü','ü','Ç','ç','Ğ','ğ','Ş','ş');
$search = array('İ','ı','Ö','ö','Ü','ü','Ç','ç','Ğ','ğ','Ş','ş');
$outputText=str_replace($search, $replace, $inputText);
return $outputText;
}

PHP convert html special chars like accents to show properly

I have a simple web page where I get some data from $_POST that users input: comments, usernames, etc. And I send them to email and save them on database. My problem is what when I send to email, doesn't matter what client is, I only see gibberish. I tried declaring the meta tag in the emails with all iso 88xx, utf8, windows, etc., but with no success. Also tried a million examples of htmlentities(), all leading to the same thing... plain gibberish. (Althought the source code shows different things sometimes, the plain text never changes).
Example code:
if (isset($_POST['name'])) {
$name = htmlentities($_POST['name'], ENT_QUOTES);
}
A result of mail() of $name (Don Quijóte) would be something like this "Don Quijóte".
Sorry if this is a repost but I just can't get this working.
Have you tried setting the headers, something like...
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:plain/text;charset=UTF-8" . "\r\n";
mail($to,$subject,$message,$headers);
This sets the encoding to UTF-8. More here:
http://www.w3schools.com/php/func_mail_mail.asp
It is surprisingly hard to send a mail in the right way. The error you get with Don Quijote is due to the fact that your string is in utf8 but it is showed with ISO8859 encoding. (That is why you get that weird A and a small 3.
I would recomend using php mailer You can get it here
It is way simpler to setup and it will be more efficient if you at some time in the future need to send out a lot of emails. (Because mail() opens and closes the connection on each call)
Also be aware that if you are using utf-8 everything should be with utf-8. Your database should be set to utf-8. You outputs to html and so on. Every step of the way you should be sure it is utf8.

Joomla UTF-8 encoding fails on opening the mail

I have a strange issue with encoding, described as follows:
the ù is now shown as ù in the email subject. The email is sent through php mail function.
When viewing the e-mail in the mailbox, it is shown correctly. However, when anybody opens the e-mail, the ù is suddenly changed to ù.
Uw contact met Meeùs
should be
Uw contact met Meeùs
i have already used the encoding.
$emailsubject contains the above mentioned email subject.
$subject=$emailsubject;
$subject=$emailsubject;
$email_message=new email_message_class;
$email_message->SetEncodedEmailHeader("To",$to_address,$to_name);
$email_message->SetEncodedEmailHeader("From",$from_address,$from_name);
$email_message->SetEncodedEmailHeader("Reply-To",$reply_address,$reply_name);
$email_message->SetHeader("Sender",$from_address);
$email_message->SetEncodedHeader("Subject",$subject,"UTF-8");
In localhost it is working properly, but in the webserver it is not working properly. In webserver also encoding is set to utf-8 by default.
What i am doing wrong?
Thanks in advance.
Your code is correct absolutely there is no error in it but its other things failing encoding. As I need message source headers and message to tell you exactly what is happening? I have further no information about are you sending the email as plain text or HTML. But there are generally two issue which are:
Missing Mime-Version
Reason for showing the character wrongly is developers forget to describe the message as MIME Version. if the message is missing the "Mime-Version" header that Internet mail standards require, Webmail will ignore the "charset" header completely, garbling the message unless it's already in the UTF-8 character set.
Showing Subject with Special Characters
As you want to show the subject with utf-8 encoding then you must encode the subject as:
//Setting the Language as Japan
mb_language("ja");
//Converting the string into Japan Encoding
$subject = mb_convert_encoding($subject, "ISO-2022-JP","AUTO");
//Now convert the string to MIME Header type
$subject = mb_encode_mimeheader($subject);
If the above mentioned things doesn't resolve the problem then request you post the RAW Headers of the Email as it will help in better way to resolve issue.
Are you test to change the charset with .htaccess ?
AddDefaultCharset UTF-8
Since you indicate in the comments you are using Joomla 1.5, it seems there is an issue with the phpmailer() library in that version that forces the character set of the mailer—on the message—to send things out using the character set setting of iso-8559-1. To fix this open up the core phpmailer() libary here:
[path to your Joomla install]/libraries/phpmailer/phpmailer.php
Around line 50 there is a setting called $CharSet. Change that to utf-8 if it’s not set to that already:
/**
* Sets the CharSet of the message.
* #var string
*/
var $CharSet = 'utf-8';
You might also want to do search of your Joomla 1.5 codebase for iso-8559-1 to see if a component or library is forcing iso-8559-1 encoding somewhere in the chain of code.
And another setting I would recommend checking is $Encoding around line 63. The default setting seems to be 8bit, but I have had to adjust that in the past to either quoted-printable or base64 to solve some mailing issues on specific setups I was working on.
/**
* Sets the Encoding of the message. Options for this are "8bit",
* "7bit", "binary", "base64", and "quoted-printable".
* #var string
*/
var $Encoding = '8bit';
I suggest you to use joomla mailer class, the could would look like this:
$mailer = JFactory::getMailer();
$mailer->setSender(array($from_address,$from_name));
$mailer->addRecipient($to_address, $to_name);
$mailer->setSubject($subject);
$mailer->setBody("BODY MESSAGE STRING");
$mailer->Send();
It's utf8 by default, and i don't see any reasons for not to use it, if you're using Joomla.

Gettext not detecting utf8 properly in PHP

I have a PHP application using Gettext as the i18n engine. The translation works fine, the only problem is that I'm having encoding issues with UTF8 characters. My PHP code to load gettext is something like this:
bindtextdomain( $domain, PATH_BASE . DS . "language" . DS );
$this->utf8Encode = strtolower($encoding) == "utf-8";
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
My templates render the pages using the utf8 charset and I've tried just about anything to load the proper charset. For the current locale I'm loading SL_sl, the names appear correctly but have issues with UTF8 chars, so where it should appear Država, it shows up Dr?ava
So, it has happened before, and now it happened again, I found the solution myself! The problem was that like I said to #bozdoz, I was converting UTF8 text already, but I didn't realized that the gettext function returned a UTF8 string, so if you do this:
$encoded = utf8_encode($utf8String);
Then you'll have a really nasty bug when $utf8String is an actual UTF8 string. Therefore I did some modifications to my code and the translation method (simplified) ended up like this:
$translation = gettext($singular);
$encoded = $this->utf8Encode ? $this->Utf8Encode($translation) : $translation;
And the Utf8Encode method is like this:
private function Utf8Encode( $text )
{
if ( mb_check_encoding($text, "utf8") == TRUE ){
return $text;
return utf8_encode($text);
}
I hope that if somebody has the same error this can help!
From the partial information I can suggest you take a look at the actual mo/po files, in poedit there are several warnings about utf8 encoding. Assuming that everything else is correct (meta, headers, etc) it's the only thing left to check
Try encoding it with utf8_encode(). I can't really tell from your code, but perhaps it could be implemented like this:
utf8_encode($domain);

Categories