doupi6737 2018-04-16 10:24
浏览 15
已采纳

PHP回复不起作用

Hope you guys can assist me on this. When someone completes my form I want the form I receive to have a "replyto" that implements the clients email address and not reply to the server.

So basically when I receive the from from the client I want to be able to say reply, where it shows the clients email address

Really hope you can help me on this

<form id="contact-form" method="post" action="contact-2.php" role="form">
    <div class="messages"></div>
    <div class="controls">
        <div class="col-md-4">
            <div class="form-group">
                <label for="form_name"></label>
                <input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="form-group">
                <label for="form_email"></label>
                <input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="form-group">
                <label for="form_subject"></label>
                <input id="form_subject" type="text" name="subject" class="form-control" placeholder="Please enter your subject*" required="required" data-error="Subject is required">
                <div class="help-block with-errors"></div>
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <label for="form_message"></label>
                <textarea id="form_message" name="message" class="form-control" placeholder="Your Message*" rows="9" required data-error="Please,leave us a message."></textarea>
                <div class="help-block with-errors"></div>
            </div>
        </div>

        <div class="col-md-4 col-md-offset-4">
            <input type="submit" class="btn btn-primary btn-block btn-lg" value="Send message">
        </div>
    </div>
</form>

<?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.
    $fromEmail = 'Demo@gmail.com';
    $fromName = 'Demo@gmail.com';
    // an email address that will receive the email with the output of the form
    $sendToEmail = 'Demo@gmail.com';
    $sendToName = 'Demo@gmail.com';
    // subject of the email
    $subject = 'contact form';
    // form field names and their translations.
    // array variable name => Text to appear in the email
    $fields = array('name' => 'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message');
    // message that will be displayed when everything is OK :)
    $okMessage = 'Contact form successfully submitted. Thank you, 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 = "<h1>You have a new message from your contact form</h1><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>Have a nice day,<br>Kind Regards,<br>Felleng Tours</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->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'];
    }
?>
  • 写回答

1条回答 默认 最新

  • douqiao2008 2018-04-16 11:03
    关注

    It's not clear where $from is defined, but you should be able to do:

    $mail->addReplyTo($_POST['email']);
    

    That will use your form's email address field as a reply-to address.

    You've got a try/catch block, but you've not told PHPMailer to use exceptions, which you can do by passing true to the constructor:

    $mail = new PHPMailer(true);
    

    That way you don't need to throw your own exceptions. You're also using an old version of PHPMailer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型