PHP sending mail from website problem

I have the following problem.

  1. Having a web page with a contact page. It includes filling a form and sending the mail. Its for people from Sweden so they will also use Swedish letters, like å ä and ö.
  2. This form send the message using PHP, see end of message
  3. It goes to an Email (IONOS is the provider) where its resent to two different E-mail accounts. One is an IONOS account, the other is a Gmail account.
  4. Arriving at IONOS account it does not show Swedish letters but some “strange” things.
  5. Arriving at G-mail, it looks OK when seeing it online, logged in to the Gmail account. But looks “strange” when arriving at my computer using Outlook 365 for seeing the message (still Gmail). But some other messages, not from my site, using Swedish letters looks OK.

So do not understand this issue. Anyone that can help?
Have a nice day
Per

<?php
  $headers = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

  $name = htmlspecialchars($_POST['name']);
  $email = htmlspecialchars($_POST['email']);
  $phone = htmlspecialchars($_POST['phone']);
  $website = htmlspecialchars($_POST['website']);
  $message = htmlspecialchars($_POST['message']);

  if(!empty($email) && !empty($message)){
    if(filter_var($email, FILTER_VALIDATE_EMAIL)){
      $receiver = "email@mail.eu"; //enter that email address where you want to receive all messages
      $subject = "From: $name <$email>";
      $body = "Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $website\n\nMessage:\n$message\n\nRegards,\n$name";
      $sender = "From: $email";
      if(mail($receiver, $subject, $body, $sender)){
         echo "Your message has been sent";
      }else{
         echo "Sorry, failed to send your message!";
      }
    }else{
      echo "Enter a valid email address!";
    }
  }else{
    echo "Email and message field is required!";
  }
?>

You never give the headers to the mail() command. So it probably will not use utf-8

1 Like