Envoyer des courriels

Résolu
J'ai un formulaire avec un bouton option afin de sélectionner un département.

J'aimerais que le département sélectionné reçoit le courriel.

Merci

Voici mon php.

<?php
/*
THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION
  • /


require 'PHPMailer-master/PHPMailerAutoload.php';

/*
  • CONFIGURE EVERYTHING HERE
  • /


// an email address that will be in the From field of the email.
$mail->CharSet="UTF-8";
$fromEmail = 'xxxx@gmail.com';
$fromName = 'Site web';

// subject of the email
$subject = 'Nouveau message fomulaire du siteweb';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('nom_id' => 'Nom', 'email_id' => 'Courriel', 'depart_id' => 'Adressé à', 'message_id' =>'Message');
$mail->Body = array('nom_id' => 'Nom -', 'email_id' => 'Courriel -', 'depart_id'=> 'Adressé à', 'message_id' =>'Message');
//, 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'

// message that will be displayed when everything is OK :)
$okMessage = 'Votre demande a été soumis avec succès. Merci, I will get back to you soon!';

// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';

/*
  • LET'S DO THE SENDING
  • /


// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

if(count($_POST) == 0) throw new \Exception('Form is empty');

$emailTextHtml = "<h3>Message du formulaire contacte du siteweb</h3><hr>";
$emailTextHtml .= "<table>";

foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$emailTextHtml .= "</table><hr>";
$emailTextHtml .= "<p>Merci! <br>Passez une belle journée.</p>";

$mail = new PHPMailer;

$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress();
$mail->addReplyTo($from);

$mail->isHTML(true);
$mail->CharSet="UTF-8";

$mail->Subject = $subject;
$mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy

if(!$mail->send()) {
throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
}

$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
// $responseArray = array('type' => 'danger', 'message' => $errorMessage);
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}

// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}

4 réponses

  1. Je ne sais comment créer une boucle sur le php pour la demande du formulaire soit dirigée directement au bon département.

  2. <select id="depart_id" name="mode_id" class="form-control">
    <option value="">Départements</option>
    <option value="Compatabilité">Comptabilité</option>
    <option value="Répartiteur">Répartiteur</option>
    <option value="Ventes">Ventes</option>
    </select>
    1. Modérateur
      là tu ne nous montres que la liste des départements......
      Je t'ai demandé le code de ton formulaire (sous entendu... complet) /

      De plus, comment sais tu, en fonction du département, vers quelle adresse tu dois envoyer ton mail ?
      Où se trouve l'information ?
      Comment la récupères tu ? Avec quel code ?

      Si tu veux de l'aide...... fais un effort sur la rédaction et les explications de tes questions.... c'est assez désagréable de venir mendier des informations qui sont pourtant évidentes à donner et sans lesquelles nous ne pouvons rien faire....
      Merci.
  3. Je m'excuse je croyais avoir mis assez d'information;

    Voici le formulaire:

    <div class="form">
              <div id="sendmessage">Your message has been sent. Thank you!</div>
              <div id="errormessage"></div>
              <form id="contact-form"  method="post" action="contact-2.php" autocomplete="on" role="form" class="form-group">
                <div class="form-row">
                  <div class="form-group col-md-6">
                    <input type="text" name="nom_id" class="form-control" id="nom_id" placeholder="Votre nom" data-rule="minlen:4" data-msg="S.V.P. entrez au moins 4 caractères" />
                    <div class="validation"></div>
                  </div>
                  <div class="form-group col-md-6">
                    <input type="email" class="form-control" name="email_id" id="email_id" placeholder="Votre courriel" data-rule="email" data-msg="S.V.P. entrez un adresse de courriel valide" />
                    <div class="validation"></div>
                  </div>
                </div>
     <div class="form-row">
                  <div class="form-group col-md-6">
                    <input type="text" name="subject_id" class="form-control" id="subject_id" placeholder="Sujet" data-rule="minlen:4" data-msg="S.V.P. entrez au moins 4 caractères" />
                    <div class="validation"></div>
                  </div>
                 <div class="form-group">
      <select id="depart_id" name="mode_id" class="form-control">
       <option value="">Départements</option>
       <option value="Compatabilité">Comptabilité</option>
       <option value="Répartiteur">Répartiteur</option>
       <option value="Ventes">Ventes</option>
      </select>
                </div>
                </div>
                <div class="form-group">
                  <textarea class="form-control" name="message_id" id="message_id" rows="5" data-rule="required" data-msg="S.V.P. entrez votre message" placeholder="Message"></textarea>
                  <div class="validation"></div>
    </div>
                <div class="text-center"><button type="submit">Envoyer</button></div>
              </form>
    


    EDIT : Correction des balises de code

    Mon PHP pour envoyer les courriel

    <?php
    
    require 'PHPMailer-master/PHPMailerAutoload.php';
    
    // an email address that will be in the From field of the email.
    $mail->CharSet="UTF-8";
    $fromEmail = 'xxxx@xxxx.com';     \*  courriel du site web en question  */
    $fromName = 'Site web';
    
    // subject of the email
    $subject = 'Nouveau message fomulaire du siteweb';
    
    // form field names and their translations.
    // array variable name => Text to appear in the email
    $fields = array('nom_id' => 'Nom', 'email_id' => 'Courriel', 'depart_id' => 'Adressé à', 'message_id' =>'Message');
    $mail->Body    = array('nom_id' => 'Nom -', 'email_id' => 'Courriel -', 'depart_id'=> 'Adressé à', 'message_id' =>'Message');
    //, 'phone' => 'Phone', 'email' => 'Email',  'message' => 'Message'
    
    // message that will be displayed when everything is OK :)
    $okMessage = 'Votre demande a été soumis avec succès. Merci, I will get back to you soon!';
    
    // If something goes wrong, we will display this message.
    $errorMessage = 'There was an error while submitting the form. Please try again later';
    
    /*
    
    
    *  LET'S DO THE SENDING*/// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);error_reporting(E_ALL & ~E_NOTICE);try{        if(count($_POST) == 0) throw new \Exception('Form is empty');        $emailTextHtml = "<h3>Message du formulaire contacte du siteweb</h3><hr>";    $emailTextHtml .= "<table>";        foreach ($_POST as $key => $value) {        // If the field exists in the $fields array, include it in the email        if (isset($fields[$key])) {            $emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";        }    }    $emailTextHtml .= "</table><hr>";    $emailTextHtml .= "<p>Merci! <br>Passez une belle journée.</p>";        $mail = new PHPMailer;        $mail->setFrom($fromEmail, $fromName);    $mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress();    $mail->addReplyTo($from);        $mail->isHTML(true);    $mail->CharSet="UTF-8";    $mail->Subject = $subject;    $mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy        if(!$mail->send()) {        throw new \Exception('I could not send the email.' . $mail->ErrorInfo);    }        $responseArray = array('type' => 'success', 'message' => $okMessage);}catch (\Exception $e){    // $responseArray = array('type' => 'danger', 'message' => $errorMessage);    $responseArray = array('type' => 'danger', 'message' => $e->getMessage());}// if requested by AJAX request return JSON responseif (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {    $encoded = json_encode($responseArray);        header('Content-Type: application/json');        echo $encoded;}// else just display the messageelse {    echo $responseArray['message'];}


    Merci d'avance pour l'aide.
    1. Contributeur
      peux-tu prendre le temps de répondre à chaque question?
      1) comment sais tu, en fonction du département, vers quelle adresse tu dois envoyer ton mail ?
      2) Où se trouve l'information ?
      3) Comment la récupères tu ? Avec quel code ?

      si tu ne comprends pas la question, merci de demander de la clarifier, au lieu de faire comme si elle n'avait pas été posée.
    2. Je m'excuse de vous avoir dérangé. Oubliez ma question.
    3. Contributeur
      @ldlouispeux-tu alors clôturer le sujet via la roue dentée à droite du titre?