Home > Back-end >  Sending information from an HTML page with PHP Mailer
Sending information from an HTML page with PHP Mailer

Time:03-05

I am creating a contact form from a bootstrap template. However, I would like to retrieve the values of the fields entered by the user in order to receive them by email on my contact address. I would like to retrieve the ID fields (name, prenom, email, contact-phone, input_from, input_to and message)

My PHP Mailer file works correctly, but I can't get the desired effect. Second error, I have predefined messages for sending or failing mail in my HTML file, how can I use it?

Can you help me with this?

PHP CODE :

                use PHPMailer\PHPMailer\PHPMailer;
                use PHPMailer\PHPMailer\Exception;
                
                require ("../PHPMailer-master/src/PHPMailer.php");
                require ("../PHPMailer-master/src/SMTP.php");
                require ("../PHPMailer-master/src/Exception.php");
                
                
                date_default_timezone_set("Europe/Paris"); 
                $mail             = new PHPMailer(); 
                $body             = "Test de PHPMailer."; 
                $mail->IsSMTP();
                $mail->SMTPAuth   = true;
                $mail->SMTPOptions = array('ssl' => array('verify_peer' => false,'verify_peer_name' => false,'allow_self_signed' => true)); // ignorer l'erreur de certificat.
                $mail->Host       = "mail.xxxx.com";  
                $mail->Port       = 587;
                $mail->Username   = "[email protected]";
                $mail->Password   = "xxxx";        
                $mail->From       = "[email protected]"; //adresse d’envoi correspondant au login entré précédemment
                $mail->FromName   = "Nouveau message sur le site "; // nom qui sera affiché
                $mail->Subject    = "Nouveau message"; // sujet
                $mail->AltBody    = "corps du message au format texte"; //Body au format texte
                $mail->WordWrap   = 50; // nombre de caractères pour le retour à la ligne automatique
                
                $mail->MsgHTML($_POST['name']); 
                $mail->MsgHTML($_POST['prenom']); 
                $mail->MsgHTML($_POST['email']); 
                $mail->MsgHTML($_POST['message']); 
                
                $mail->AddReplyTo("votre mail","votre nom");
                //$mail->AddAttachment("./examples/images/phpmailer.gif");// pièce jointe si besoin
                $mail->AddAddress("xxxxxxxxxxxxxxxxx");
                $mail->IsHTML(true); // envoyer au format html, passer a false si en mode texte 
                
                if(!$mail->Send()) {
                    echo "Mailer Error: " . $mail->ErrorInfo;
                } else {
                    echo "Le message à bien été envoyé";
                } 
                ?>

HTML CODE :

          <form action="contact-2.php" method="post" role="form" >
            <div >
              <div >
                <input type="text" name="name"  id="name" placeholder="Nom" required>
              </div>
              <div >
                <input type="text"  name="prenom" id="prenom" placeholder="Prénom" required>
              </div>
              <div >
                <input type="email"  name="email" id="email" placeholder="Email" required>
              </div>
            
            <div >
              <input type="tel"  name="subject" id="contact-phone" placeholder="Téléphone" required>
            </div>
              <div >
                  <input type="text"  id="input_from" placeholder="A partir du :" required>
              </div>

              <div >
                  <input type="text"  id="input_to" placeholder="Au :" required>
              </div>
            </div>

            <div >
              <textarea  name="message" rows="6" placeholder="Message" required></textarea>
            </div>
          
            <div >
              <div >Envoi en cours</div>
              <div ></div>
              <div >Votre message a été envoyé. Merci !</div>
            </div>
            <div ><button type="submit">Envoyer</button></div>
          </form>
        </div>

CodePudding user response:

Bonjour Seazy. This won't work:

$mail->MsgHTML($_POST['name']); 
$mail->MsgHTML($_POST['prenom']); 
$mail->MsgHTML($_POST['email']); 
$mail->MsgHTML($_POST['message']); 

You will end up with a message that contains only what's in the message field, and msgHTML, won't be much help here. Keeping it simple, you need to do something like this (using a plain-text message):

$mail->Body = <<<EOT
{$_POST['name']} 
{$_POST['prenom']}
{$_POST['email'])}
{$_POST['message']} 
EOT;

It looks like you've based your code on a very old example, so take a look at the contact form example provided with PHPMailer.

If you instead want to include these values in a template, you need to look at loading those template files using PHP's output buffering and include statement, along the lines of:

ob_start();
include 'template.php';
$html = ob_get_contents();
ob_end_clean();
$mail->msgHTML($html);
  • Related