Bon je vous donne le code complet, mais c'est assez conséquent!
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/phpmailer/class.phpmailer.php");
class Soumission {
public $prenom;
public $nom;
public $adresse;
public $ville;
public $province;
public $cp;
public $pays;
public $tel;
public $courriel;
public $departement;
public $produit;
public $sujet;
public $message;
public $upload;
public $allowedType = array(
".jpg" => "image/jpeg",
".png" => "image/png",
".gif" => "image/gif",
".pdf" => "application/pdf"
);
private $upload_path;
private $maxFilesize = 2097152;
private $validDepartements = array("vente" => "Vente",
"infos" => "Informations",
"commentaires" => "Commentaires",
"support" => "Support Technique",
"photo" => "Photo/Video");
private $smtpEmail = "noreply@spypoint.com";
private $smtpUsername = "noreply@spypoint.com";
private $smtpPassword = "qwc157nr";
private $smtpHost = "mail.spypoint.com";
private $smtpPort = 25;
private $toEmail = array("vente" => "sales@spypoint.com",
"infos" => "info@spypoint.com",
"commentaires" => "info@spypoint.com",
"support" => "tech@spypoint.com",
"photo" => "pcharriere@spypoint.com");
private $error = array(
"prenom" => false,
"nom" => false,
"adresse" => false,
"ville" => false,
"provinve" => false,
"cp" => false,
"pays" => false,
"tel" => false,
"courriel" => false,
"departement" => false,
"sujet" => false,
"produit" => false,
"message" => false,
"upload" => false
);
public function getError($fieldName) {
if($this->error[$fieldName] != false) {
echo '<span class="alert">'.$this->error[$fieldName].'</span>';
}
}
public function __construct(){
if(isset($_POST['send'])){
$this->prenom = htmlspecialchars($_POST["prenom"]);
$this->nom = htmlspecialchars($_POST["nom"]);
$this->adresse = htmlspecialchars($_POST["adresse"]);
$this->ville = htmlspecialchars($_POST["ville"]);
$this->province = htmlspecialchars($_POST["province"]);
$this->cp = htmlspecialchars($_POST["cp"]);
$this->pays = htmlspecialchars($_POST["pays"]);
$this->tel = htmlspecialchars($_POST["tel"]);
$this->courriel = htmlspecialchars($_POST["courriel"]);
$this->departement = htmlspecialchars($_POST["departement"]);
$this->produit = htmlspecialchars($_POST["produit"]);
$this->sujet = htmlspecialchars($_POST["sujet"]);
$this->message = stripslashes($_POST["message"]);
$this->message = htmlspecialchars($this->message);
$this->message = nl2br($this->message);
$this->upload = !empty($_FILES['upload']['name']) ? $_FILES['upload'] : FALSE;
}
}
public function ErrorCount() {
$errorCount = 0;
foreach($this->error as $e) {
if($e != false) {
$errorCount++;
}
}
return $errorCount;
}
public function validation(){
$this->error['courriel'] = !preg_match("/^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/", $this->courriel)?FIELD_INVALID [decu]$this->error['courriel'] !== false?$this->error['courriel']:false);
$this->error['cp'] = !preg_match("#(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)#i", $this->cp)?FIELD_INVALID [decu]$this->error['cp'] !== false?$this->error['cp']:false);
$this->error['tel'] = !preg_match("/^[+]?[01]?[- .]?(\\([2-9]\\d{2}\\)|[2-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}$/", $this->tel)?FIELD_INVALID [decu]$this->error['tel'] !== false?$this->error['tel']:false);
$this->error['departement'] = !isset($this->validDepartements[$this->departement])?FIELD_INVALID [decu]$this->error['departement'] !== false?$this->error['departement']:false);
$this->error['produit'] = ($this->departement == "support" || $this->departement == "photo") && strlen($this->produit) >= 25?FIELD_INVALID [decu]$this->error['produit'] !== false?$this->error['produit']:false);
$this->error['prenom']= (strlen($this->prenom) > 20)?FIELD_LENGTH [decu]$this->error['prenom'] !== false?$this->error['prenom']:false);
$this->error['nom']= (strlen($this->nom) > 20)?FIELD_LENGTH [decu]$this->error['nom'] !== false?$this->error['nom']:false);
$this->error['sujet']= (strlen($this->sujet) > 50)?FIELD_LENGTH [decu]$this->error['sujet'] !== false?$this->error['sujet']:false);
$this->error['courriel'] = strlen($this->courriel) > 30?FIELD_LENGTH [decu]$this->error['courriel'] !== false?$this->error['courriel']:false);
$this->error['adresse']= strlen($this->adresse) > 50?FIELD_LENGTH [decu]$this->error['adresse'] !== false?$this->error['adresse']:false);
$this->error['ville']= strlen($this->ville) > 20?FIELD_LENGTH [decu]$this->error['ville'] !== false?$this->error['ville']:false);
$this->error['cp']= strlen($this->cp) > 7?FIELD_LENGTH [decu]$this->error['cp'] !== false?$this->error['cp']:false);
$this->error['province'] = strlen($this->province) > 20?FIELD_LENGTH [decu]$this->error['province'] !== false?$this->error['province']:false);
$this->error['message'] = strlen($this->message) > 500?FIELD_LENGTH [decu]$this->error['message'] !== false?$this->error['message']:false);
$this->error['prenom'] = empty($this->prenom)?FIELD_REQUIRED [decu]$this->error['prenom'] !== false?$this->error['prenom']:false);
$this->error['nom'] = empty($this->nom)?FIELD_REQUIRED [decu]$this->error['nom'] !== false?$this->error['nom']:false);
$this->error['courriel'] = empty($this->courriel)?FIELD_REQUIRED [decu]$this->error['courriel'] !== false?$this->error['courriel']:false);
$this->error['departement'] = $this->departement == "null"?FIELD_REQUIRED [decu]$this->error['departement'] !== false?$this->error['departement']:false);
$this->error['sujet'] = empty($this->sujet)?FIELD_REQUIRED [decu]$this->error['sujet'] !== false?$this->error['sujet']:false);
$this->error['message'] = empty($this->message)?FIELD_REQUIRED [decu]$this->error['message'] !== false?$this->error['message']:false);
$this->error['tel'] = empty($this->tel)?FIELD_REQUIRED [decu]$this->error['tel'] !== false?$this->error['tel']:false);
$this->error['ville'] = empty($this->ville)?FIELD_REQUIRED [decu]$this->error['ville'] !== false?$this->error['ville']:false);
$this->error['pays'] = $this->pays == "null"?FIELD_REQUIRED [decu]$this->error['pays'] !== false?$this->error['pays']:false);
$this->error['produit'] = ($this->departement == "support" || $this->departement == "photo") && empty($this->produit)?FIELD_REQUIRED [decu]$this->error['produit'] !== false?$this->error['produit']:false);
$this->error['upload'] = ($this->departement == "photo" && $this->upload == FALSE) ? PLEASE_UPLOAD : ($this->error['upload'] !== false?$this->error['upload']:false);
if($this->upload !== FALSE) {
$ext = strrpos($this->upload['name'], ".");
$ext = strtolower(substr($this->upload['name'], $ext, strlen($this->upload['name'])));
$this->error['upload'] = !isset($this->allowedType[$ext])?UPLOAD_WRONG_TYPE:false;
if($this->ErrorCount() > 0)
return;
$this->error['upload'] = !$this->allowedType[$ext] == $this->upload['type']?UPLOAD_WRONG_TYPE:false;
if($this->ErrorCount() > 0)
return;
$this->error['upload'] = filesize($this->upload['tmp_name']) > $this->maxFilesize?UPLOAD_MAX_FILESIZE:false;
if($this->ErrorCount() > 0)
return;
$this->upload_path = "/tmp/mail_" . time() . $ext;
$this->error['upload'] = !move_uploaded_file($this->upload['tmp_name'], $this->upload_path)?UPLOAD_MOVE:false;
if($this->ErrorCount() > 0)
return;
}
}
public function sendMail() {
if($this->ErrorCount() == 0) {
$this->prenom = stripslashes($this->prenom);
$this->nom = stripslashes($this->nom);
$this->adresse = stripslashes($this->adresse);
$this->ville = stripslashes($this->ville);
$this->province = stripslashes($this->province);
$this->cp = stripslashes($this->cp);
$this->pays = stripslashes($this->pays);
$this->tel = stripslashes($this->tel);
$this->courriel = stripslashes($this->courriel);
$this->produit = stripslashes($this->produit);
$this->sujet = stripslashes($this->sujet);
$this->message = stripslashes($this->message);
$to = $this->toEmail[$this->departement];
$subject = "[" . $this->validDepartements[$this->departement] . "] Demande de contact sur Site Web";
$body = "<div><strong>Nom complet : </strong> " . ($this->prenom) . " " . $this->nom . "<div>";
if(!empty($this->adresse)) { $body .= "<div><strong>Adresse : </strong> " . ($this->adresse) . "<div>"; }
if(!empty($this->ville)) { $body .= "<div><strong>Ville : </strong> " . ($this->ville) . "<div>"; }
if(!empty($this->province)) {$body .= "<div><strong>Province/état : </strong> " . ($this->province) . "<div>"; }
if(!empty($this->cp)) {$body .= "<div><strong>Code Postal : </strong> " . ($this->cp) . "<div>"; }
if(!empty($this->pays)) {$body .= "<div><strong>Pays : </strong> " . ($this->pays) . "<div>"; }
$body .= "<div><strong>Téléphone : </strong> " . ($this->tel) . "<div>";
$body .= "<div><strong>Courriel : </strong> " . ($this->courriel) . "<div>";
if(!empty($this->produit)) { $body .= "<div><strong>Nom produit : </strong> " . ($this->produit) . "<div><br /><hr />"; }
$body .= "<div><strong>Sujet : </strong> " . ($this->sujet) . "</div><hr />";
$body .= "<br /><font face=\"courrier\">".($this->message)."</font>";
$body = $body;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Host = $this->smtpHost;
$mail->Port = $this->smtpPort;
$mail->Username = $this->smtpUsername;
$mail->Password = $this->smtpPassword;
$mail->AddReplyTo($this->courriel, $this->prenom . " " . $this->nom);
$mail->SetFrom("noreply@spypoint.com", "Formulaire Contact");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->CharSet = 'utf-8';
$mail->AddAddress($to);
$mail->IsHTML(true);
if($this->upload !== FALSE) {
$mail->AddAttachment($this->upload_path, $this->upload['name']);
}
if(!$mail->Send()) {
$this->error['upload'] = UPLOAD_SEND;
return false;
} else {
return true;
}
}
$this->DeleteFile();
}
public function DeleteFile() {
if($this->upload !== FALSE && file_exists($this->upload_path)) {
@unlink($this->upload_path);
}
}
}
?>