Problem with mail() function and hebrew

Discussion in 'PHP' started by artipron, Feb 1, 2010.

  1. #1
    I've created a 'contact us' form using the php code below :

    <?php
    $sender = $_REQUEST['sender'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    $mail = $_REQUEST['mail'] ;
    $phone = $_REQUEST['phone'] ;
      
    $totalmessage = "
            Name:        $sender  \n
            Subject:    $subject  \n
            Message:   $message  \n
            Email:       $mail \n
            Phone:     $phone \n";
    
       mail( "my@mail.com", "Website", $totalmessage, "From: $email" );
       echo "<img src=\"mail.gif\" title=\"Success\" alt=\"Success\" />"; 
    
    ?>
    PHP:
    It works. the problem is that when the user input hebrew letters to the form,
    i can't see it correctly. Also, the "Name, Subject, Message" etc. should be
    in hebrew and i wrote it in english just for you so you'll be able to understand the code.

    I read lots of tutorials about encoding using $headers, but i couldn't implement it to the code. i really need your help on this.

    Thanks.
     
    artipron, Feb 1, 2010 IP
  2. Joak1m

    Joak1m Peon

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Send message in UTF-8 enconding. Use headers, something like this:

    
    $headers .= "From: $email\r\n"; 
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/plain; charset=utf-8\r\n"; 
    $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
    
    PHP:
    And change this part too:
    
    mail( "my@mail.com", "Website", $totalmessage, $headers);
    
    PHP:
    Not sure if the syntax is right.
     
    Joak1m, Feb 1, 2010 IP
  3. artipron

    artipron Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    $sender = $_REQUEST['sender'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    $mail = $_REQUEST['mail'] ;
    $phone = $_REQUEST['phone'] ;
      
    $totalmessage = "
            Name:        $sender  \r\n
            Subject:    $subject  \r\n
            Message:   $message  \r\n
            Email:       $mail \r\n
            Phone:     $phone \r\n";
    		
    $headers .= "From: $mail\r\n"; 
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/plain; charset=utf-8\r\n"; 
    $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
    
       mail( "my@mail.com", "Website", $totalmessage, $headers );
       echo "<img src=\"mail.gif\" title=\"Success\" alt=\"Success\" />"; 
    
    ?>
    PHP:
    This is the code now. still doesn't work. :(
     
    artipron, Feb 1, 2010 IP
  4. Joak1m

    Joak1m Peon

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The code doesn't work or the hebrew doesn't show correctly? What kind of encoding does the page has, where is the submit form?
     
    Joak1m, Feb 1, 2010 IP
  5. Joak1m

    Joak1m Peon

    Messages:
    135
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Or maybe it's better to use windows-1255 charset, what is meant for hebrew...

    
    <?php
    header('Content-Type: text/html; charset=windows-1255');
    
    $sender = $_REQUEST['sender'] ;
    $subject = $_REQUEST['subject'] ;
    $message = $_REQUEST['message'] ;
    $mail = $_REQUEST['mail'] ;
    $phone = $_REQUEST['phone'] ;
      
    $totalmessage = "
            Name:        $sender  \r\n
            Subject:    $subject  \r\n
            Message:   $message  \r\n
            Email:       $mail \r\n
            Phone:     $phone \r\n";
            
    $headers .= "From: $mail\r\n"; 
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/plain; charset=windows-1255\r\n"; 
    $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
    
       mail( "my@mail.com", "Website", $totalmessage, $headers );
       echo "<img src=\"mail.gif\" title=\"Success\" alt=\"Success\" />"; 
    
    ?>
    
    PHP:
    Maybe this one works. And if not, then try replacing those to charset windows-1255 with utf-8.
     
    Joak1m, Feb 1, 2010 IP
  6. artipron

    artipron Peon

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Changed "Text/Plain" to "Text/HTML" and it works!!

    Thanks mate. :)
     
    artipron, Feb 1, 2010 IP
  7. yoes_san

    yoes_san Peon

    Messages:
    443
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    noted that this line

    $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
    Code (markup):
    may cause your html mail body to go weird, for example, the body of:
    "<table width='100'>" will becomes "<table width='>"

    Edit: Actually I remember you don't even need that line, just try it and see then
     
    yoes_san, Feb 2, 2010 IP