简体   繁体   English

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

[英]How to link custom “Sent” Page via PHPMailer

I've recently started working with PHPMailer to email my contactforms and after some puzzling, it works great. 我最近开始与PHPMailer合作,通过电子邮件发送我的联系表,经过一番困惑之后,它的工作原理非常好。 The only 'downside' is that the user gets redirected after sending an email. 唯一的“缺点”是用户在发送电子邮件后会被重定向。 For example: 例如:

If one would fill in the form here , they will be redirected to another page like this one when the message sent succesfully. 如果一个人将填写表格这里 ,他们将被重定向到像另一页这一个当消息发送成功地。

My HTML for my form is the following: 我的表单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>

The php for my form is the following: 我的表格的PHP如下:

<?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);

}
?>

As you can see, an error and success message was already coded in but if redirecting is something that should happen, I want to code a custom HTML-page. 如您所见,错误和成功消息已经被编码,但是如果重定向应该发生,我想编写一个自定义HTML页面。 Anybody that can help me set the redirect so I can start coding? 有人可以帮助我设置重定向以便我开始编码吗?

Thanks for your time! 谢谢你的时间!

You are not doing any redirects here. 您在这里没有进行任何重定向。 Your form tag contains action="php/contact.php" , so that's where the form will submit to. 您的表单标签包含action="php/contact.php" ,因此表单将提交到该位置。 If you want to go somewhere else after processing the form submission and sending a message, you can do a redirect like this: 如果要在处理表单提交并发送消息后想去其他地方,可以执行以下重定向:

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

If the form contains errors, you could redirect back to the form, passing error messages in the URL parameters. 如果表单包含错误,则可以重定向回表单,并在URL参数中传递错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM