使用 PHP Mailer 从 HTML 页面发送信息

发布于 2025-01-10 21:34:40 字数 4828 浏览 0 评论 0原文

我正在从引导模板创建联系表单。 但是,我想检索用户输入的字段的值,以便通过我的联系地址上的电子邮件接收它们。 我想检索 ID 字段(姓名、名称、电子邮件、联系电话、input_from、input_to 和消息)

我的 PHP Mailer 文件工作正常,但无法获得所需的效果。 第二个错误,我在 HTML 文件中预定义了用于发送或失败邮件的消息,我该如何使用它?

你能帮我解决这个问题吗?

PHP 代码:

                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 代码:

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

              <div class="form-group col-md-6">
                  <input type="text" class="form-control" id="input_to" placeholder="Au :" required>
              </div>
            </div>

            <div class="form-group">
              <textarea class="form-control" name="message" rows="6" placeholder="Message" required></textarea>
            </div>
          
            <div class="my-3">
              <div class="loading">Envoi en cours</div>
              <div class="error-message"></div>
              <div class="sent-message">Votre message a été envoyé. Merci !</div>
            </div>
            <div class="text-center"><button type="submit">Envoyer</button></div>
          </form>
        </div>

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" class="php-email-form">
            <div class="row">
              <div class="form-group col-md-6">
                <input type="text" name="name" class="form-control" id="name" placeholder="Nom" required>
              </div>
              <div class="form-group col-md-6">
                <input type="text" class="form-control" name="prenom" id="prenom" placeholder="Prénom" required>
              </div>
              <div class="form-group col-md-6">
                <input type="email" class="form-control" name="email" id="email" placeholder="Email" required>
              </div>
            
            <div class="form-group col-md-6">
              <input type="tel" class="form-control" name="subject" id="contact-phone" placeholder="Téléphone" required>
            </div>
              <div class="form-group col-md-6">
                  <input type="text" class="form-control" id="input_from" placeholder="A partir du :" required>
              </div>

              <div class="form-group col-md-6">
                  <input type="text" class="form-control" id="input_to" placeholder="Au :" required>
              </div>
            </div>

            <div class="form-group">
              <textarea class="form-control" name="message" rows="6" placeholder="Message" required></textarea>
            </div>
          
            <div class="my-3">
              <div class="loading">Envoi en cours</div>
              <div class="error-message"></div>
              <div class="sent-message">Votre message a été envoyé. Merci !</div>
            </div>
            <div class="text-center"><button type="submit">Envoyer</button></div>
          </form>
        </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-01-17 21:34:40

卓悦希兹。这是行不通的:

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

您最终会收到一条仅包含 message 字段中内容的消息,而 msgHTML 在这里不会有太大帮助。保持简单,您需要执行以下操作(使用纯文本消息):

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

看起来您的代码基于一个非常古老的示例,因此请查看 PHPMailer 提供的联系表单示例

如果您想在模板中包含这些值,则需要考虑使用 PHP 的输出缓冲和 include 语句加载这些模板文件,如下所示:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文