Non-ascii email subject and header encoding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ciuin

    Non-ascii email subject and header encoding

    Hi all,

    I need to mail() emails with user input that does contain non-ascii
    (umlauts, accents) and non-latin (cyrillic) characters in the
    "Subject:" and "From:" headers. I understand that they are typically
    encoded in UTF8 like this:

    =?UTF-8?B?w5Z0emkg0J/RgNC40LLQtdGC?=

    but I cannot find a PHP function to encode the input string in this
    way. utf8_encode gives me garbled char soup, so what do you use?

    Thanks.

  • Ciuin

    #2
    Re: Non-ascii email subject and header encoding

    On Mar 27, 11:55 am, "Ciuin" <c...@gmx.dewro te:
    Hi all,
    >
    I need to mail() emails with user input that does contain non-ascii
    (umlauts, accents) and non-latin (cyrillic) characters in the
    "Subject:" and "From:" headers. I understand that they are typically
    encoded in UTF8 like this:
    >
    =?UTF-8?B?w5Z0emkg0J/RgNC40LLQtdGC?=
    >
    but I cannot find a PHP function to encode the input string in this
    way. utf8_encode gives me garbled char soup, so what do you use?
    >
    Thanks.
    Ah well, bad thinking on my part.

    Instead of utf8_encode I need base64_encode, of course (as the "...?
    B?..." in the code tells me). So this:

    $from = "From: =?UTF-8?B?" . base64_encode($ _POST['name']) . "?= <" . $
    $_POST['email'] . ">\n";

    produces a correctly encoded header. Same for subject.

    Hope it helps someone.

    Comment

    • Erwin Moller

      #3
      Re: Non-ascii email subject and header encoding

      Ciuin wrote:
      On Mar 27, 11:55 am, "Ciuin" <c...@gmx.dewro te:
      >Hi all,
      >>
      >I need to mail() emails with user input that does contain non-ascii
      >(umlauts, accents) and non-latin (cyrillic) characters in the
      >"Subject:" and "From:" headers. I understand that they are typically
      >encoded in UTF8 like this:
      >>
      >=?UTF-8?B?w5Z0emkg0J/RgNC40LLQtdGC?=
      >>
      >but I cannot find a PHP function to encode the input string in this
      >way. utf8_encode gives me garbled char soup, so what do you use?
      >>
      >Thanks.
      >
      Ah well, bad thinking on my part.
      Was it?
      Not really.

      Encoding/charsets/headers/content-type/UTF/unicode/etc ALWAYS gives me a
      headache. :-/
      Confusing stuff, especially when you have to consider a whole range of
      receiving clients (different browsers, emailclients, etc).

      Regards,
      Erwin Moller
      >
      Instead of utf8_encode I need base64_encode, of course (as the "...?
      B?..." in the code tells me). So this:
      >
      $from = "From: =?UTF-8?B?" . base64_encode($ _POST['name']) . "?= <" . $
      $_POST['email'] . ">\n";
      >
      produces a correctly encoded header. Same for subject.
      >
      Hope it helps someone.

      Comment

      • Tim Roberts

        #4
        Re: Non-ascii email subject and header encoding

        "Ciuin" <ciuin@gmx.dewr ote:
        >
        >Instead of utf8_encode I need base64_encode, of course (as the "...?
        >B?..." in the code tells me). So this:
        >
        >$from = "From: =?UTF-8?B?" . base64_encode($ _POST['name']) . "?= <" . $
        >$_POST['email'] . ">\n";
        >
        >produces a correctly encoded header. Same for subject.
        For completeness, allow me to point out that you can also use
        quoted-printable encoding here (you'd use =?utf-8?Q? instead of ...?B?).
        Quoted-printable encoding has the "advantage" that ASCII characters survive
        unchanged, so if there are ASCII words, they can be read even in their
        encoded form.

        On the other hand, strings with many non-ASCII characters grow more in
        quoted-printable than in base64. Plus, there is no
        "quoted_printab le_encode" in the standard library, although sources are
        available.
        --
        Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • Manuel Lemos

          #5
          Re: Non-ascii email subject and header encoding

          Hello,

          on 03/27/2007 06:55 AM Ciuin said the following:
          Hi all,
          >
          I need to mail() emails with user input that does contain non-ascii
          (umlauts, accents) and non-latin (cyrillic) characters in the
          "Subject:" and "From:" headers. I understand that they are typically
          encoded in UTF8 like this:
          >
          =?UTF-8?B?w5Z0emkg0J/RgNC40LLQtdGC?=
          >
          but I cannot find a PHP function to encode the input string in this
          way. utf8_encode gives me garbled char soup, so what do you use?
          That is binary q-encoding. It is not quoted-printable but it is similar
          for message headers. There is a whole RFC on that subject.

          Yoy may want to try the MIME message that can be used to compose and
          send messages and supports headers with non-ASCII characters encoded as
          UTF-8 or any other encoding.

          Take a look at the test_multibyte_ message.php example script. It
          explains how to send messages in Japanese with encoding ISO-2022-JP, but
          you can change that for UTF-8 to support characters of all idioms.




          --

          Regards,
          Manuel Lemos

          Metastorage - Data object relational mapping layer generator
          ✅「官方网站」mk体育拥有各种免费又安全的资源,因为亚洲当中中国玩家人口基数的众多,mk体育创造了中国网络游戏的神话,全球首推免费在线点播体育直播。


          PHP Classes - Free ready to use OOP components written in PHP
          Free PHP Classes and Objects 2025 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials

          Comment

          Working...