Author Topic: email users in UTF-8  (Read 4624 times)

0 Members and 1 Guest are viewing this topic.

Offline alekseyn1

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • My Project
email users in UTF-8
« on: February 07, 2010, 06:41:23 PM »
Hello everybody!

I have an issue that I can not resolve on my own (not enough knowledge)...

I need to send any outgoing e-mail from my 4images installation in UFT-8 format... I have modified the email.php as follows. (I use mail function of PHP and not SMTP)

Code: [Select]
....

$this->subject = iconv('windows-1251', 'UTF-8', $subject);

....

$header .= 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";

.....

// in function prepare_text($message) {

    return iconv('windows-1251', 'UTF-8', $message);

...

function send_email() {
    if ($this->use_smtp) {
      return ($this->smtp_mail($this->to, $this->subject, $this->body, $this->create_header())) ? 1 : 0;
    }
    else {
      //original line was - return (mail($this->to, $this->subject, $this->body, $this->create_header())) ? 1 : 0;
 return (mail($this->to, '=?UTF-8?B?'.base64_encode($this->subject).'?=', $this->body, $this->create_header())) ? 1 : 0;
 
    }
  }


I hope there are pro-s out there who could help me out with this.... because in MS Oulook for example it works great, in AOL and Gmail.... but most of my users are on Mail.ru servers and those DO NOT display messages send using the above modifications correctly....

Any suggestions or hints would be greatly appreciated!
Aleksey
« Last Edit: February 07, 2010, 06:53:53 PM by alekseyn1 »

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: email users in UTF-8
« Reply #1 on: February 07, 2010, 07:43:59 PM »
They see an empty email?
perhaps it's something with mail.ru itself?
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline alekseyn1

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • My Project
Re: email users in UTF-8
« Reply #2 on: February 07, 2010, 08:00:45 PM »
They see an empty email?
perhaps it's something with mail.ru itself?

no... they actually see a message, but the encoding is not recognized... moreover, the switches inside the mail.ru interface can not decode the message.....  :? :? :?

Does the code look ok?

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: email users in UTF-8
« Reply #3 on: February 07, 2010, 09:21:41 PM »
Here is my result:
1) since I'm using plain text in email template I had to replace
	
  
$header .= 'MIME-Version: 1.0' "\r\n" 'Content-type: text/html; charset=UTF-8' "\r\n";

with
	
  
$header .= 'MIME-Version: 1.0' "\r\n" 'Content-type: text/plain; charset=UTF-8' "\r\n";

otherwise whole message was displayed in one line.

2) I had to make sure that my site uses windows-1251 encoding (at least the page with email form), otherwise it was sending HTML entities for non-latin letters (тест)

other then that everything looked fine in my tests, even on mail.ru

[EDIT]
attached little fixed version that converts html entities into letters which makes it independent from page encoding.
« Last Edit: February 08, 2010, 03:11:23 AM by V@no »
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline alekseyn1

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • My Project
Re: email users in UTF-8
« Reply #4 on: February 08, 2010, 12:25:14 AM »
Thanks very much,

I have forgotten to put headers into a second statement after checking if from is empty... here is the correct function in case someone runs into the same problem

Code: [Select]
  function create_header() {
    global $config;
    $header = "";
    if (empty($this->from)) {

      $header .= 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
  $header .= sprintf("Return-Path: %s\r\n", $config['site_email']);
      $header .= sprintf("From: %s\r\n", $config['site_email']);
    }
    else {
  $header .= 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
      $header .= $this->from;

    }

    if (!empty($this->bcc) && !$this->use_smtp) {
      $bcc_list = "";
      foreach ($this->bcc as $key => $val) {
        $bcc_list .= (($bcc_list != "") ? ", " : "").$val;
      }
      $header .= sprintf("Bcc: %s\r\n", $bcc_list);
    }
    return $header;
  }