I'm trying to send开发者_JAVA百科 a mail containing ÅÄÖ characters (Swedish).
I've tried changing the mail header to UTF-8 and iso-8859-1, none of which works. I've also tried wrapping the text in utf8_encode() as well as mb_encode_mimeheader(), with no success. In some cases i get chinese characters instead, not really what i want.
I just tried using htmlentities() with no result.
$values = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'ref' => $this->input->post('ref'),
'sex' => $this->input->post('sex'),
'age' => $this->input->post('age'),
'question' => $this->input->post('msg'),
'ip' => $_SERVER['REMOTE_ADDR']
);
$to = '@live.se';
$subject = $values['name'] . ' har skickat en fråga!';
$message = 'Namn:'.$values['name']." \r\n";
$message .= "\r\nEmail:".$values['email']." \r\n";
$message .= "\r\nKön:".(($values['sex'] == 'f') ? 'Kvinna' : 'Man')." \r\n";
$message .= "\r\nÅlder:".$values['age']." \r\n";
$message .= "\r\nReferensnummer:".$values['ref']." \r\n";
$message .= "\r\nMeddelande: \r\n".$values['question'];
$headers = 'From: noreply@.se' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'Reply-To: ' .$this->input->post('email') . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = htmlentities($message);
if(@mail($to, $subject, $message, $headers)) {
if($this->input->is_ajax_request()) {
echo 1;
}else {
$data['message']['header'] = 'Tack så mycket!';
$data['message']['content'] = 'Din fråga skickades utan problem! Jag återkommer snarast möjligt.';
echo $this->load->view('includes/header', array(), true);
echo $this->load->view('message', $data['message'], true);
echo $this->load->view('includes/footer', array(), true);
}
}else {
$fail = true;
}
I don't know if this is the best way to achieve this. But I used to use mail like this:
mail($to, "=?utf-8?B?".base64_encode($subject)."?=", $message, $headers);
A very old thread, but did not seem to have a good answer. If the encoding of the page which contains the form is encoded as utf-8, then the function utf8_decode("your åäö-text");
is your friend.
While trying to figure this out, i remembered i was using Codeigniter, and thought maybe, just maybe, codeigniter had a built in Email library. And, it turns out it does!
So i gave it a shot, and found out it had a serious bug in it. Luckily, i found a fixed version of the file here: http://codeigniter.com/forums/viewthread/87108/P15/#753867
And now it works. I don't know exactly what codeigniter does to make it work, but i'm glad it does!
tl;dr: i don't have a real solution to the problem.
This.
$message = preg_replace('/[^(\x20-\x7F)]*/','', $message);
精彩评论