检测PHP Mailer错误并使用jquery在网站上显示消息 [英] Detect PHP Mailer error and print the message in the website using jquery

查看:64
本文介绍了检测PHP Mailer错误并使用jquery在网站上显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PhpMailer发送电子邮件.在我自己的脚本中,我想在下面的if语句中解析一些javascript.请注意,我已启用html.

I am using PhpMailer to send emails. Inside my own script I want to parse some javascript inside the below if statement. Note that I have html enabled.

当未发送电子邮件 时,我想在.done函数中解析一些未发送电子邮件的JavaScript.例如complete.html("Message Not Sent!");发送电子邮件时,我要显示电子邮件已发送.在php文件或javascript中,我该怎么做?在哪里做得更好?

When the email is not sent I want to parse some javascript inside the .done function that the email is not sent. eg complete.html("Message Not Sent!"); When the email is sent, I want to show that the email is sent. How can I do that and where is better to do that, inside php file or inside the javascript?

 var form = $('#contact');
    form.submit(function(event) {
      event.preventDefault();
      var complete = $('#formAppend');
      var $form = $(this);
      var name = $("#fname").val();
      var email = $("#email").val();
      var Message = $("#msg").val();
      var countryoption = $("#country").val();
      $.ajax({
          type: 'POST',
          url: '../sendemail.php',
          data: {
            Name: name,
            Email: email,
            message: Message,
            Country: countryoption
          },
          beforeSend: function() {
            complete.html('Message is Sending...').fadeIn();
          }
        })
        .done(function(data) {
//This should change depending on the php if statement at the bottom.
          complete.html("Message Sent");
        });
    }); //end contact form

<form id="contact" method="post" action="#ss">
  <div id="formAppend"></div>
  <input type="text" id="fname" name="firstname" placeholder="Το όνομα σας.." required>
  <input type="email" id="email" name="email" placeholder="Το email σας.." required>
  <select id="country" required>
      <option class="w3-center" value="" disabled selected value>-- Χώρα --</option>
      <option value="Κυπρος">Κύπρος</option>
      <option value="Ελλάδα">Ελλάδα</option>
      <option value="Άλλο">Άλλη</option>
  </select>
  <textarea id="msg" name="message" placeholder="Γράψε το μήνυμα.." style="height:200px;max-height:400px;min-height:100px;" required></textarea>
  <div id="op">
    <button type="submit" style="" class="btn btn-primary img-responsive"> Αποστολή</button> </div>
</form>

PHP:

<?php
require 'PHPMailer-master/PHPMailerAutoload.php';



    if(empty($_POST['Email'])){
        $_POST['Email']= "";
    }
    if(empty($_POST['Name'])){
        $_POST['Name']= "";
    }

    if(empty($_POST['message'])){
        $_POST['message']= "";
    }


if (isset($_POST["message"]) && !empty($_POST["message"])) {

   $mymail=smtpmailer("webdominar1@gmail.com",$_POST['Email'], $_POST['Name'], $_POST['message']);    


}else{  
    header('Location: http://webdominar.xyz'); exit();

}



function smtpmailer($to, $from, $from_name, $body) {
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->Debugoutput = 'html';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = '';  
    $mail->Password = '';          
    $mail->SetFrom($from, $from_name);
    $mail->Subject = "Εμβολιασμός Δέντρων ~ Φόρμα Επικοινωνίας ~ $body ";
    $mail->CharSet  = 'UTF-8';
    $mail->isHTML(true);                                  // Set email format to HTML
    $Country = $_POST["Country"]; 
    $mail->Body = "BLABLA ";//end email body




    $mail->AddAddress($to);
//send the message, check for errors
if (!$mail->send()) { //Message not sent
    echo "Mailer Error: " . $mail->ErrorInfo;

} else {//Message sent!
    echo "Well done $from_name, your message has been sent!\nWe will reply to the following email: $from"
            . "<br>Your Message: $body";
}




} //end function smtpmailer



?>

推荐答案

将PHP的结尾更改为:

Change the end of your PHP to:

header("Content-type:application/json"); 
$message = "Well done ".$from_name.", your message has been sent!<br/>We will reply to the following email:".$from."<br>Your Message: ".$body;
if (!$mail->send()) {     
  echo '{"error":"'.$mail->ErrorInfo.'"}';
} else {
  echo '{"success":"'.json_encode($message).'"}';
} 

然后您可以在.done中完成此操作:

Then you can do this in the .done:

.done(function(data) {
    if (data.error) { 
     complete.html(data.error).addClass("error"); 
    }
    else  
      complete.html(JSON.parse(data.success));
    }
});

这篇关于检测PHP Mailer错误并使用jquery在网站上显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆