Problème d'encodage avec un formulaire POST

salvator64
Invité n'ayant pas de compte PHPfrance

20 avr. 2010, 19:14

Bonjour à tous j'ai un petit problème dans la réception des mails.
Le formulaire est en flash et envoi par l'intermédiaire d'un POST les informations.
Les accents et les apostrophes s'affichent en code.
Voici mon code si quelqu'un peux m'aider svp
Merci pour votr aide
<?php

require_once('./init.php');

function sendMail($to, $fromaddress, $subject, $body, $attachments=false)
{
	$eol="\r\n";
	$mime_boundary=md5(time());

	# Common Headers
	$headers .= "From: ".$fromaddress.$eol;
	$headers .= "Reply-To: ".$fromaddress.$eol;
	$headers .= "Return-Path: ".$fromaddress.$eol;
	$headers .= "Message-ID: <".time()."-".$fromaddress.">".$eol;
	$headers .= "X-Mailer: PHP v".phpversion().$eol;

	# Boundry for marking the split & Multitype Headers
	$headers .= 'MIME-Version: 1.0'.$eol;
	$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol.$eol;

	# Open the first part of the mail
	$msg = "--".$mime_boundary.$eol;

	$htmlalt_mime_boundary = $mime_boundary."_htmlalt";
	$msg .= "Content-Type: multipart/alternative; boundary=\"".$htmlalt_mime_boundary."\"".$eol.$eol;

	# Text Version
	$msg .= "--".$htmlalt_mime_boundary.$eol;
	$msg .= "Content-Type: text/plain; charset=utf-8".$eol;
	$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
	$msg .= strip_tags(str_replace("<br>", "\n", substr($body, (strpos($body, "<body>")+6)))).$eol.$eol;

	# HTML Version
	$msg .= "--".$htmlalt_mime_boundary.$eol;
	$msg .= "Content-Type: text/html; charset=utf-8".$eol;
	$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
	$msg .= $body.$eol.$eol;

	//close the html/plain text alternate portion
	$msg .= "--".$htmlalt_mime_boundary."--".$eol.$eol;

	if ($attachments !== false && count($attachments['name']) > 0)
	{
		for ($i = 0; $i < count($attachments['name']); $i++) {
			if (is_file($attachments["tmp_name"][$i]))
			{
				# File for Attachment
				$handle = fopen($attachments["tmp_name"][$i], 'rb');
				$f_contents = fread($handle, filesize($attachments["tmp_name"][$i]));
				$f_contents = chunk_split(base64_encode($f_contents));
				fclose($handle);

				# Attachment
				$msg .= "--".$mime_boundary.$eol;
				$msg .= "Content-Type: ".$attachments["type"][$i]."; name=\"".$attachments['name'][$i]."\"".$eol;
				$msg .= "Content-Transfer-Encoding: base64".$eol;
				$msg .= "Content-Description: ".$attachments['name'][$i].$eol;
				$msg .= "Content-Disposition: attachment; filename=\"".$attachments['name'][$i]."\"".$eol.$eol;
				$msg .= $f_contents.$eol.$eol;
			}
		}
	}

	$msg .= "--".$mime_boundary."--".$eol.$eol;

	ini_set(sendmail_from, $fromaddress);
	$mail_sent = mail($to, $subject, $msg, $headers);

	ini_restore(sendmail_from);

	return $mail_sent;
}

parse_str(trim(file_get_contents(DIR_ROOT.$_POST['pathForm'])), $out);
foreach ($out as $k=>$v) {
	$out[$k] = urldecode($v);
}

$message = '';
for ($i = 1; $i <= 4; $i ++){
	$message .= '<b>'.$out['field'.$i].'</b>: '.nl2br(Core::StrT($_POST['field'.$i])).'<br />';
}

sendMail($out['email'], 'contact.form@'.$_SERVER['SERVER_NAME'], 'Contact form', $message);

ViPHP
ViPHP | 1996 Messages

21 avr. 2010, 09:14

Bonjour,

Tout d'abord les p'tits boutons sur le dessus permettent d'encapsuler le codes afin qu'il soit plus lisible (c'est mieux pour mes p'tits yeux).
Ensuite généralement on mets un vrai titre et un vrai pseudo. NMQMQ est -il un vrai pseudo ? (ma foi p'tet bien lol)
Sinon pour tes caractères accentués. Tu envois ton email en format HTML ou en texte sous la forme UTF-8 par la ligne
"Content-Type: text/plain; charset=utf-8".$eol;
ou
$msg .= "Content-Type: text/plain; charset=utf-8".$eol;
Or est ce que $body (par la ligne
$msg .= $body.$eol.$eol;
ou
$msg .= strip_tags(str_replace("<br>", "\n", substr($body, (strpos($body, "<body>")+6)))).$eol.$eol;
) est compatible UTF-8 ?

Essaye par exemple pour la version texte :
$msg .= strip_tags(str_replace("<br>", "\n", utf8_encode(substr($body, (strpos($body, "<body>")+6))))).$eol.$eol;
It is nice to be important but it is more important to be nice
http://www.aureuswebfactory.fr

Avatar du membre
Administrateur PHPfrance
Administrateur PHPfrance | 13231 Messages

26 avr. 2010, 14:09

Modération :
Merci d'utiliser un titre clair et qui correspond bien à ta demande.

Merci de prendre le temps de lire les règlements.
Connaître son ignorance est la meilleure part de la connaissance
Pour un code lisible : n'hésitez pas à sauter des lignes et indenter

twitter - site perso - Github - Zend Certified Engineer