Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“如何链接自定义”;“发送”;页面通过PHPMailer_Php_Html_Phpmailer - Fatal编程技术网

“如何链接自定义”;“发送”;页面通过PHPMailer

“如何链接自定义”;“发送”;页面通过PHPMailer,php,html,phpmailer,Php,Html,Phpmailer,我最近开始与PHPMailer合作,通过电子邮件发送我的联系人表单,经过一些困惑之后,效果非常好。唯一的“缺点”是用户在发送电子邮件后会被重定向。例如: 如果有人填写表单,他们将被重定向到另一个页面,就像消息成功发送时一样 我的表单的HTML如下所示: <form class="form ajax-contact-form" method="post" action="php/contact.php"> <div class="alert alert-success h

我最近开始与PHPMailer合作,通过电子邮件发送我的联系人表单,经过一些困惑之后,效果非常好。唯一的“缺点”是用户在发送电子邮件后会被重定向。例如:

如果有人填写表单,他们将被重定向到另一个页面,就像消息成功发送时一样

我的表单的HTML如下所示:

<form class="form ajax-contact-form" method="post" action="php/contact.php">
    <div class="alert alert-success hidden" id="contact-success">
      &nbsp; <strong>Votre message a &eacute;t&eacute; envoy&eacute; avec
      succ&egrave;s!</strong> Merci, nous contacterons vous d&egrave;s que possible.
    </div>

    <div class="alert alert-danger hidden" id="contact-error">
      &nbsp; <strong>Votre message n'a pas &eacute;t&eacute; envoy&eacute;.</strong> Vous
      avez tout rempli correctement?
    </div>

    <div class="row col-p10">
      <div class="col-sm-6">
        <label class="mb10"><input type="text" name="name_" id="name_" required="" class=
        "form-control" placeholder="Votre nom" /></label>
      </div>

      <div class="col-sm-6">
        <label class="mb10"><input type="text" name="adress_" id="subject_" required=""
        class="form-control" placeholder="Votre adresse" /></label>
      </div>
    </div>

    <div class="row col-p10">
      <div class="col-sm-6">
        <label class="mb10"><input type="number" name="zipcode_" id="zipcode_" required=
        "" class="form-control" placeholder="Code postal" /></label>
      </div>

      <div class="col-sm-6">
        <label class="mb10"><input type="text" name="city_" id="city_" required="" class=
        "form-control" placeholder="Ville ou commune" /></label>
      </div>
    </div>

    <div class="row col-p10">
      <div class="col-sm-6">
        <label class="mb10"><input type="tel" name="phone_" id="phone_" required=""
        class="form-control" placeholder=
        "Votre num&eacute;ro de t&eacute;l&eacute;phone" /></label>
      </div>

      <div class="col-sm-6">
        <label class="mb10"><input type="email" name="email_" id="email_" required=""
        class="form-control" placeholder="Votre adresse d'email" /></label>
      </div>
    </div>

    <div class="row">
      <div class="col-sm-12">
        <label><select name="select_" id="select_" required="" class="form-control">
          <option value="Invalid">
            Choisi un option
          </option>

          <option value="CV Ketel huren">
            Je veux louer une chaudi&egrave;re
          </option>

          <option value="CV Ketel kopen">
            Je veux acheter une chaudi&egrave;re
          </option>

          <option value="Ik wens meer informatie">
            Je veux plus d'infos
          </option>
        </select></label>
      </div>
    </div><label>
    <textarea name="message_" id="message_" cols="30" rows="10" class="form-control"
    placeholder="Votre message (optionel)">
</textarea></label>

    <div class="mb40"></div>

    <div class="clearfix">
      <div class="pull-left">
        <button type="submit" class="btn btn-icon btn-e">Envoi</button>
      </div>
    </div>
  </form>
<?php

session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');

// Include PHPMailer class
include("./PHPMailer/PHPMailerAutoload.php");

// grab reCaptcha library
require_once "./reCaptcha/recaptchalib.php";

///////////////////////////////////////////////////////////////////////

// Enter your email address below.
$to_address = "info@cvketelshuren.com"; 

// Enter your secret key (google captcha)
$secret = "Leftout for apparent reasons";

//////////////////////////////////////////////////////////////////////


// Verify if data has been entered
if(!empty($_POST['name_']) || !empty($_POST['adress_']) || !empty($_POST['zipcode_']) || !empty($_POST['city_']) || !empty($_POST['phone_']) || !empty($_POST['email_']) || !empty($_POST['select_'])) {

    $name = $_POST['name_'];
    $adress = $_POST['adress_'];
    $zipcode = $_POST['zipcode_'];
    $city = $_POST['city_'];
    $phone = $_POST['phone_'];
    $email = $_POST['email_'];
    $select = $_POST['select_'];

    // Configure the fields list that you want to receive on the email.
    $fields = array(
        0 => array(
            'text' => 'Naam',
            'val' => $_POST['name_']
        ),
        1 => array(
            'text' => 'Adres',
            'val' => $_POST['adress_']
        ),
        2 => array(
            'text' => 'Stad',
            'val' => $_POST['zipcode_']." ".$_POST['city_']
        ),
        3 => array(
            'text' => 'Select',
            'val' => $_POST['select_']
        ),
        4 => array(
            'text' => 'Telefoonnummer',
            'val' => $_POST['phone_']
        ),
        5 => array(
            'text' => 'Mailadres',
            'val' => $_POST['email_']
        ),
        6 => array(
            'text' => 'Bericht',
            'val' => $_POST['message_']
        )
    );


    $message = "Waarde collega,<br>Er werd een contactformulier ingevuld op de site cvketelshuren.com<br>Gelieve hieronder de gegevens van de klant terug te vinden.<br>";
    foreach($fields as $field) {
        $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
    }


    $mail = new PHPMailer;

    $mail->IsSMTP(); // Set mailer to use SMTP
    $mail->SMTPDebug = 0; // Debug Mode


    // If you don't receive the email, try to configure the parameters below:

    $mail =  new PHPMailer;
        $mail->Host = 'mailout.one.com';
        $mail->SMTPAuth = true;
        $mail->Username = '***************';
        $mail->Password = '************';
        $mail->SMTPSecure = false;
        $mail->Port = 25;


    $mail->From = $email;
    $mail->FromName = $name;
    $mail->AddAddress($to_address); 
    $mail->AddReplyTo($email, $name);


    if (!empty($_POST['send_copy_'])) {
        $mail->AddAddress($email);
    }


    $mail->IsHTML(true); // Set email format to HTML
    $mail->CharSet = 'UTF-8';

    $mail->Subject = 'Vraag via CV Ketels Huren [NL]';
    $mail->Body    = $message;


    // Google CAPTCHA
    $resp = null; // empty response
    $reCaptcha = new ReCaptcha($secret); // check secret key

    // if submitted check response
    if ($_POST["g-recaptcha-response"]) {
        $resp = $reCaptcha->verifyResponse(
            $_SERVER["REMOTE_ADDR"],
            $_POST["g-recaptcha-response"]
        );
    }

    // if captcha is ok, send email
    if ($resp != null && $resp->success) {

        if($mail->Send()) {
            $result = array ('response'=>'success');
        } else {
            $result = array ('response'=>'error' , 'error_message'=> $mail->ErrorInfo);
        }

    } else {
        $result = array ('response'=>'error' , 'error_message'=>'Google ReCaptcha did not work');
    }

    echo json_encode($result);

} else {

    $result = array ('response'=>'error' , 'error_message'=>'Data has not been entered');
    echo json_encode($result);

}
?>

Votre消息aé;té;特使&埃库特;avec
成功与失败;s谢谢,我们的联系人请原谅;这是完全可能的。
Votre message n'a pasé;té;特使éVOU
avez tout rempli矫正?
Choisi un选项
我是乔迪和埃格拉夫的朋友;重新
我是乔迪和埃格拉夫的父亲;重新
Je veux plus d'infos
恩沃伊
我的表单的php如下所示:

<form class="form ajax-contact-form" method="post" action="php/contact.php">
    <div class="alert alert-success hidden" id="contact-success">
      &nbsp; <strong>Votre message a &eacute;t&eacute; envoy&eacute; avec
      succ&egrave;s!</strong> Merci, nous contacterons vous d&egrave;s que possible.
    </div>

    <div class="alert alert-danger hidden" id="contact-error">
      &nbsp; <strong>Votre message n'a pas &eacute;t&eacute; envoy&eacute;.</strong> Vous
      avez tout rempli correctement?
    </div>

    <div class="row col-p10">
      <div class="col-sm-6">
        <label class="mb10"><input type="text" name="name_" id="name_" required="" class=
        "form-control" placeholder="Votre nom" /></label>
      </div>

      <div class="col-sm-6">
        <label class="mb10"><input type="text" name="adress_" id="subject_" required=""
        class="form-control" placeholder="Votre adresse" /></label>
      </div>
    </div>

    <div class="row col-p10">
      <div class="col-sm-6">
        <label class="mb10"><input type="number" name="zipcode_" id="zipcode_" required=
        "" class="form-control" placeholder="Code postal" /></label>
      </div>

      <div class="col-sm-6">
        <label class="mb10"><input type="text" name="city_" id="city_" required="" class=
        "form-control" placeholder="Ville ou commune" /></label>
      </div>
    </div>

    <div class="row col-p10">
      <div class="col-sm-6">
        <label class="mb10"><input type="tel" name="phone_" id="phone_" required=""
        class="form-control" placeholder=
        "Votre num&eacute;ro de t&eacute;l&eacute;phone" /></label>
      </div>

      <div class="col-sm-6">
        <label class="mb10"><input type="email" name="email_" id="email_" required=""
        class="form-control" placeholder="Votre adresse d'email" /></label>
      </div>
    </div>

    <div class="row">
      <div class="col-sm-12">
        <label><select name="select_" id="select_" required="" class="form-control">
          <option value="Invalid">
            Choisi un option
          </option>

          <option value="CV Ketel huren">
            Je veux louer une chaudi&egrave;re
          </option>

          <option value="CV Ketel kopen">
            Je veux acheter une chaudi&egrave;re
          </option>

          <option value="Ik wens meer informatie">
            Je veux plus d'infos
          </option>
        </select></label>
      </div>
    </div><label>
    <textarea name="message_" id="message_" cols="30" rows="10" class="form-control"
    placeholder="Votre message (optionel)">
</textarea></label>

    <div class="mb40"></div>

    <div class="clearfix">
      <div class="pull-left">
        <button type="submit" class="btn btn-icon btn-e">Envoi</button>
      </div>
    </div>
  </form>
<?php

session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');

// Include PHPMailer class
include("./PHPMailer/PHPMailerAutoload.php");

// grab reCaptcha library
require_once "./reCaptcha/recaptchalib.php";

///////////////////////////////////////////////////////////////////////

// Enter your email address below.
$to_address = "info@cvketelshuren.com"; 

// Enter your secret key (google captcha)
$secret = "Leftout for apparent reasons";

//////////////////////////////////////////////////////////////////////


// Verify if data has been entered
if(!empty($_POST['name_']) || !empty($_POST['adress_']) || !empty($_POST['zipcode_']) || !empty($_POST['city_']) || !empty($_POST['phone_']) || !empty($_POST['email_']) || !empty($_POST['select_'])) {

    $name = $_POST['name_'];
    $adress = $_POST['adress_'];
    $zipcode = $_POST['zipcode_'];
    $city = $_POST['city_'];
    $phone = $_POST['phone_'];
    $email = $_POST['email_'];
    $select = $_POST['select_'];

    // Configure the fields list that you want to receive on the email.
    $fields = array(
        0 => array(
            'text' => 'Naam',
            'val' => $_POST['name_']
        ),
        1 => array(
            'text' => 'Adres',
            'val' => $_POST['adress_']
        ),
        2 => array(
            'text' => 'Stad',
            'val' => $_POST['zipcode_']." ".$_POST['city_']
        ),
        3 => array(
            'text' => 'Select',
            'val' => $_POST['select_']
        ),
        4 => array(
            'text' => 'Telefoonnummer',
            'val' => $_POST['phone_']
        ),
        5 => array(
            'text' => 'Mailadres',
            'val' => $_POST['email_']
        ),
        6 => array(
            'text' => 'Bericht',
            'val' => $_POST['message_']
        )
    );


    $message = "Waarde collega,<br>Er werd een contactformulier ingevuld op de site cvketelshuren.com<br>Gelieve hieronder de gegevens van de klant terug te vinden.<br>";
    foreach($fields as $field) {
        $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
    }


    $mail = new PHPMailer;

    $mail->IsSMTP(); // Set mailer to use SMTP
    $mail->SMTPDebug = 0; // Debug Mode


    // If you don't receive the email, try to configure the parameters below:

    $mail =  new PHPMailer;
        $mail->Host = 'mailout.one.com';
        $mail->SMTPAuth = true;
        $mail->Username = '***************';
        $mail->Password = '************';
        $mail->SMTPSecure = false;
        $mail->Port = 25;


    $mail->From = $email;
    $mail->FromName = $name;
    $mail->AddAddress($to_address); 
    $mail->AddReplyTo($email, $name);


    if (!empty($_POST['send_copy_'])) {
        $mail->AddAddress($email);
    }


    $mail->IsHTML(true); // Set email format to HTML
    $mail->CharSet = 'UTF-8';

    $mail->Subject = 'Vraag via CV Ketels Huren [NL]';
    $mail->Body    = $message;


    // Google CAPTCHA
    $resp = null; // empty response
    $reCaptcha = new ReCaptcha($secret); // check secret key

    // if submitted check response
    if ($_POST["g-recaptcha-response"]) {
        $resp = $reCaptcha->verifyResponse(
            $_SERVER["REMOTE_ADDR"],
            $_POST["g-recaptcha-response"]
        );
    }

    // if captcha is ok, send email
    if ($resp != null && $resp->success) {

        if($mail->Send()) {
            $result = array ('response'=>'success');
        } else {
            $result = array ('response'=>'error' , 'error_message'=> $mail->ErrorInfo);
        }

    } else {
        $result = array ('response'=>'error' , 'error_message'=>'Google ReCaptcha did not work');
    }

    echo json_encode($result);

} else {

    $result = array ('response'=>'error' , 'error_message'=>'Data has not been entered');
    echo json_encode($result);

}
?>

您没有在此处执行任何重定向。表单标签包含
action=“php/contact.php”
,因此表单将提交到该标签。如果在处理表单提交并发送消息后要转到其他地方,可以执行如下重定向:

header('Location: some_other_url/page.php');

如果表单包含错误,您可以重定向回表单,在URL参数中传递错误消息。

谢谢您的评论,但我不确定在哪里可以输入此标题?这是我目前发送电子邮件的代码:(HTML文本域中的PHP)。该代码看起来应该是从XHR调用的,而不是从与其他代码不相关的人类表单提交中调用的。您应该在问题中包含相关代码,而不是在外部网站上。只是更新了fiddle以包含contact.php的完整代码,请检查此处:创建了一个fiddle,因为无法将您链接到我的测试服务器上的源代码