<- Back

Correcting special character problem ( Ä, Ö, ä, ö ) when sending email with PHP

Send email with scandinavian characters

Posted in: Learned along the way
By: Blockcoder ( Admin ) / August 7th, 2011
Stats: 4 responses / Views: 592

Take the variables in store

First, you usually has a form where the string data comes from:

    //take your form data in variables
    //depending on what method your are using in your form, use
    //( $_GET, $_POST, $_REQUEST )

    $name = $_REQUEST['name'];
    $email = $_REQUEST['email'];
    $message = $_REQUEST['message'];

Now that you have stored your form data in variables, lets change things a little bit. We are inserting the variables into one variable called “whole_message”. Its easier to handle one variable than three.

           $whole_message = "Name: ".$name."\n
                             Email: ".$email." \n\n\n
                             Message: \n\n ".$message."";

Let us do the conversion

Now, we have the three variables sticked in one variable, lets erase those badass special characters ( ä, ö, Ö, Ä ) from our “whole_message”. What we are doing, is that we change the special characters to html-entities with php function “str_replace”.

    str_replace( 'ä', '&auml;', $whole_message );
    str_replace( 'ö', '&ouml;', $whole_message );
    str_replace( 'Ä', '&Auml;', $whole_message );
    str_replace( 'Ö', '&Ouml;', $whole_message );

Okay, how str_replace works? First it searches for ‘ä’ character from $whole_message, if it finds ‘ä’ it will replace it with its html-entity, $auml; and so on.

Remember to add headers

We could be now ready, but when sending email with php, you need also take care of the headers. This time we need the Content-Type because we are handling the those specials characters. Lets create the headers. Charset will be UTF-8.

  $headers = "From: ".$email."\n";
  $headers .= "Content-Type: text/plain; charset=UTF-8\n";
  $headers .= "Content-Transfer-Encoding: 8bit\n";

Here’s the headers. All we need to do here anymore is to send the email.

Send your email with special charaters

if( mail ( "recipient_email",  "Contact - ".$name,  $whole_message, $headers ) ){
      echo "<script type='text/javascript'>alert('Message was sent!');</script>";

      echo "<script type='text/javascript'>alert('There was an error, please try again.');</script>";

If the email was sent succesfully it will notify you by success message, if not, it will show an error message.

Put the recipient email address to its correct place and you should be happy sending an email without problems! Remember, this is just one example to get rid of the problem, not the only one.

Leave a Reply

Your email address will not be published. Required fields are marked *


* Copy this password:

* Type or paste password here:

3,562 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>