Fonction envoi de mail php qui permet de passer le filtre anti spam

Contenu du snippet

Tout est dans le titre :)

C'est une fonction qui vous permettra de passer outre les filtres antispam des hébergeurs de messagerie tels que Hotmail Yahoo ou Gmail.

Source / Exemple :


<?php
date_default_timezone_set("Europe/Paris");
function envoyermail($mail, $emailsubject, $contenu, $fromname, $frommail, $organisation, $textorhtml, $timezone) {
	error_reporting(0);
	if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { $eol="\r\n"; }
	elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { $eol="\r"; }
	else { $eol="\n"; }

    $headers = 'Reply-To: '.$fromname.' <'.$frommail.'>'.$eol; 
    $headers .= 'Return-Path: '.$fromname.' <'.$frommail.'>'.$eol; 
	$headers .= 'From: '.$fromname.' <'.$frommail.'>'.$eol;
	$headers .= 'Organization: '.$organisation.$eol;
	if($textorhtml=="0") { $headers .= 'Content-Type: text/plain'.$eol; }
	else { $headers .= "Content-Type: text/html; charset=iso-8859-1".$eol; }
	$headers .= "X-Priority: normal".$eol;
	$headers .= "X-MSMail-Priority: Normal".$eol;
	$headers .= "Importance: High".$eol;
	$headers .= "X-Mailer: PHP v" . phpversion().$eol;
	$headers .= "MIME-Version: 1.0".$eol;
	$headers .= "Delivery-date: ".date("D, j M Y H:i:s ".$timezone).$eol;
	$headers .= "X-Originating-IP: [".getenv("REMOTE_ADDR")."]".$eol;
	$headers .= "X-Sender-IP: " . $_SERVER["REMOTE_ADDR"].$eol;
	$headers .= "Content-Transfer-Encoding: 8bit".$eol; 
	$headers .= 'Sender: '.$frommail.$eol; 

	ob_start();
	echo stripslashes($contenu);
	$body=ob_get_contents(); ob_end_clean();
	
	ini_set(sendmail_from,$frommail);
	mail($mail, $emailsubject, $body, $headers);
	ini_restore(sendmail_from);
	error_reporting(-1);
}
envoyermail("mail@destinataire.tld", "SUJET DU MAIL", "<h3>CONTENU HTML DE L'EMAIL</h3>", "NOM EXPEDITEUR", "mail@expediteur.tld", "Organisation", "1", "+0100");
?>

VOICI UN EXEMPLE PRATIQUE :

<?php
date_default_timezone_set("Europe/Paris");
function envoyermail($mail, $emailsubject, $contenu, $fromname, $frommail, $organisation, $textorhtml, $timezone) {
	error_reporting(0);
	if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { $eol="\r\n"; }
	elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { $eol="\r"; }
	else { $eol="\n"; }
	$headers = 'Reply-To: '.stripslashes($fromname).' <'.$frommail.'>'.$eol;
	$headers .= 'Return-Path: '.stripslashes($fromname).' <'.$frommail.'>'.$eol;
	$headers .= 'From: '.stripslashes($fromname).' <'.$frommail.'>'.$eol;
	$headers .= 'Organization: '.$organisation.$eol;
	if($textorhtml=="0") { $headers .= 'Content-Type: text/plain'.$eol; }
	else { $headers .= "Content-Type: text/html; charset=iso-8859-1".$eol; }
	$headers .= "X-Priority: normal".$eol;
	$headers .= "X-MSMail-Priority: Normal".$eol;
	$headers .= "Importance: High".$eol;
	$headers .= "X-Mailer: PHP v" . phpversion().$eol;
	$headers .= "MIME-Version: 1.0".$eol;
	$headers .= "Delivery-date: ".date("D, j M Y H:i:s ".$timezone).$eol;
	$headers .= "X-Originating-IP: [".getenv("REMOTE_ADDR")."]".$eol;
	$headers .= "X-Sender-IP: " . $_SERVER["REMOTE_ADDR"].$eol;
	$headers .= "Content-Transfer-Encoding: 8bit".$eol;
	$headers .= 'Sender: '.$frommail.$eol;
	ob_start();
	echo stripslashes($contenu);
	$body=ob_get_contents(); ob_end_clean();
	ini_set('sendmail_from',$frommail);
	mail($mail, stripslashes($emailsubject), $body, $headers);
	ini_restore('sendmail_from');
	error_reporting(-1);
}
$err=''; $succ="";
if(isset($_POST['envoyer'])) {
	$mailto=trim($_POST['mail']);
	$verif=$mailto; $t1=explode("@",$verif); $t2=explode(".",$t1[1]); $t3=explode(" ",$verif); $t5=explode(".@",$verif); $t6=explode("@.",$verif);
	if((sizeof ($t1) == 2) && (sizeof ($t2) > 1) && (sizeof ($t3) == 1) && (sizeof ($t5) == 1) && (sizeof ($t6) == 1)){
		$subject=addslashes(trim($_POST['sujet']));
		$contenu=addslashes($_POST['message']);
		$nomprenom=addslashes(strtoupper(trim($_POST['nom'])).' '.ucwords(strtolower(trim($_POST['prenom']))));
		envoyermail("MON_MAIL@POUR_LA_RECEPTION.FR", $subject, $contenu, $nomprenom, $mailto, "MA SOCIETE", "1", "+0100");
		$succ="Envoi réussi !";
	}
	else { $err='Le format de l\'E-Mail saisie est incorrect'; }
}
?>

<form action="" method="post">
<?php if($err) { ?><font color="red"><?php echo $err; ?></font><?php } ?>
<?php if($succ) { ?><font color="green"><?php echo $succ; ?></font><?php } ?>
<table cellpadding="0" cellspacing="0" border="0">
<tr><td>Nom</td><td><input type="text" name="nom" /></td></tr>
<tr><td>PrÉnom</td><td><input type="text" name="prenom" /></td></tr>
<tr><td>E-Mail</td><td><input type="text" name="mail" /></td></tr>
<tr><td>Sujet</td><td><input type="text" name="sujet" /></td></tr>
<tr><td valign="top">Message</td><td><textarea name="message"></textarea></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="envoyer" value="Valider" /></td></tr>
</table>
</form>

Exemple MASS MAILER :

<?php
date_default_timezone_set("Europe/Paris");
function envoyermail($mail, $emailsubject, $contenu, $fromname, $frommail, $organisation, $textorhtml, $timezone) {
	error_reporting(0);
	if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { $eol="\r\n"; }
	elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { $eol="\r"; }
	else { $eol="\n"; }
	$headers = 'Reply-To: '.stripslashes($fromname).' <'.$frommail.'>'.$eol;
	$headers .= 'Return-Path: '.stripslashes($fromname).' <'.$frommail.'>'.$eol;
	$headers .= 'From: '.stripslashes($fromname).' <'.$frommail.'>'.$eol;
	$headers .= 'Organization: '.$organisation.$eol;
	if($textorhtml=="0") { $headers .= 'Content-Type: text/plain'.$eol; }
	else { $headers .= "Content-Type: text/html; charset=iso-8859-1".$eol; }
	$headers .= "X-Priority: normal".$eol;
	$headers .= "X-MSMail-Priority: Normal".$eol;
	$headers .= "Importance: High".$eol;
	$headers .= "X-Mailer: PHP v" . phpversion().$eol;
	$headers .= "MIME-Version: 1.0".$eol;
	$headers .= "Delivery-date: ".date("D, j M Y H:i:s ".$timezone).$eol;
	$headers .= "X-Originating-IP: [".getenv("REMOTE_ADDR")."]".$eol;
	$headers .= "X-Sender-IP: " . $_SERVER["REMOTE_ADDR"].$eol;
	$headers .= "Content-Transfer-Encoding: 8bit".$eol;
	$headers .= 'Sender: '.$frommail.$eol;
	ob_start();
	echo stripslashes($contenu);
	$body=ob_get_contents(); ob_end_clean();
	ini_set('sendmail_from',$frommail);

	$emails=split("\n", $mail);
	for($i=0; $i<count($emails); $i++) {
		$verif=trim($emails[$i]); $t1=explode("@",$verif); $t2=explode(".",$t1[1]); $t3=explode(" ",$verif); $t5=explode(".@",$verif); $t6=explode("@.",$verif);
		if((sizeof ($t1) == 2) && (sizeof ($t2) > 1) && (sizeof ($t3) == 1) && (sizeof ($t5) == 1) && (sizeof ($t6) == 1)){
			mail(trim($emails[$i]), stripslashes($emailsubject), $body, $headers);
			echo "Envoyé à ".$emails[$i]."<br />";
		}
	}
	ini_restore('sendmail_from');
	error_reporting(-1);
}
$err=''; $succ="";
if(isset($_POST['envoyer'])) {
	$mailto=trim($_POST['mail']);
	$subject=addslashes(trim($_POST['sujet']));
	$contenu=addslashes($_POST['message']);
	envoyermail($mailto, $subject, $contenu, "NOM PRENOM", "admin@mymail.com", "MA SOCIETE", "1", "+0100");
	$succ="Envoi réussi !";
}
?>

<form action="" method="post">
<?php if($err) { ?><font color="red"><?php echo $err; ?></font><?php } ?>
<?php if($succ) { ?><font color="green"><?php echo $succ; ?></font><?php } ?>
<table cellpadding="0" cellspacing="0" border="0">
<tr><td valign="top">E-Mails</td><td><textarea name="mail"></textarea></td></tr>
<tr><td>Sujet</td><td><input type="text" name="sujet" /></td></tr>
<tr><td valign="top">Message</td><td><textarea name="message"></textarea></td></tr>
<tr><td colspan="2" align="center"><input type="submit" name="envoyer" value="Valider" /></td></tr>
</table>
</form>

Conclusion :


Bon courage :)

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.