php mail() not sending chinese text correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragon52
    New Member
    • Jun 2009
    • 72

    php mail() not sending chinese text correctly

    Hi there,

    I am writing php to send email containing Chinese text. The chinese displays correctly depending on different mail servers, it seems.

    I can read the chinese text if I open the email using webmail on my telco's website (Telstra's Bigpond in Australia) or Yahoo mail. If I look at the same email using MS Outlook then the chinese text is just rubbish.

    So I experimented and wrote (VS2008) the code using the Smtp.Client (Microsoft System.Net.Mail ) rather then php. This method gives me the correct chinese text in both webmail and Outlook.

    Can someone explain to me what is going on here?

    Given I must use php, how do I reproduce smtp.client code in php?

    In the php code below how much of the $Header stuff do I actually need?

    Sorry for such a long winded question and thanks in advance.

    Here is my codes, both php and c#

    Code:
    <?php
    $charset = "utf-8";
    $to = "$_POST[EmailAddress]";
    $subject = "Student Enrollment";
    $body = "尊敬的家 Welcome to the Sydney";
    
    //HEADERS
    $header .= "X-Mailer: Whatever you want\n";
    $header .= "Return-Path: <someperson@example.com>\n";
    $header .= "MIME-Version: 1.0\n";
    $header .= "From:denis@yahoo.com.au\n";
    $header .= "X-Accept-Language: cn\n";
    $header .= "Content-Type: text/plain; charset={$charset}\n";
    $header .= "Content-Transfer-Encoding: 8bit\n";
    
        if (mail($to,$subject,$body,$header))
        {
            echo("<p>A confirmation email has been sent.</p>");
        }
        else
        {
            echo("<p>We are unable to send you an email.</p>");
        }
    ?>
    
    ////////////////////////////////
    
                MailMessage mail = new MailMessage();
    
                mail.From = new MailAddress("denis@yahoo.com.au");
                mail.To.Add(address);
                mail.Subject = subject;
                mail.Body = body;
    
                SmtpClient smtp = new SmtpClient("mail.bigpond.com");
                try
                {
                    smtp.Send(mail);
                }
    Last edited by Dormilich; Sep 19 '09, 12:49 AM. Reason: Please use [code] tags when posting code
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    I suspect this is because of PHP's lax handling of Unicode characters. You might try out PHP6 as one of it's main goals is to improve multi-byte and unicode character support.


    Mark.

    Comment

    • dragon52
      New Member
      • Jun 2009
      • 72

      #3
      Mark,

      Thanks very much for your response.

      Unfortunately I don't control what version of php to use.

      Since Smtp.Client in the .Net environment works do you think it will work if I write the php using smtp?

      I know hardly anything about smtp. I have searched the net about smtp in php and what I have found mentions changing the configuration and seem complicated.

      Can you point me to some smtp code which I can use fairly easily?

      Thanks
      Denis

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        a fairly easy (to use and well documented) PHP Mail Script is SwiftMailer, though I didn’t try sending chinese text with it.

        Comment

        • dragon52
          New Member
          • Jun 2009
          • 72

          #5
          Thanks Dormilich.

          SwiftMailer looks interesting. I will have a look.

          Comment

          Working...