【问题标题】:PHP and HTML email form not sending successfully [duplicate]PHP和HTML电子邮件表单未成功发送[重复]
【发布时间】:2017-09-04 15:16:19
【问题描述】:

我有一个电子邮件表单,其代码如下:

(Live site)。

  <link href="https://antimalwareprogram.co/oxygen%20/template/css/responsive.css" rel="stylesheet">
  <link id="css-preset" href="https://antimalwareprogram.co/oxygen%20/template/css/presets/preset1.css" rel="stylesheet">
  <link href="https://antimalwareprogram.co/oxygen%20/template/css/main.css" rel="stylesheet">
  <link href="https://antimalwareprogram.co/oxygen%20/template/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://antimalwareprogram.co/oxygen%20/template/css/lightbox.css" rel="stylesheet">
  <link href="https://antimalwareprogram.co/oxygen%20/template/css/font-awesome.min.css" rel="stylesheet">
  <link href="https://antimalwareprogram.co/oxygen%20/template/css/animate.min.css" rel="stylesheet"> 

  <link href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700' rel='stylesheet' type='text/css'>
 <script type="text/javascript" src="https://antimalwareprogram.co/oxygen%20/template/js/jquery.js">    </script>
  <script type="text/javascript" src="https://antimalwareprogram.co/oxygen%20/template/js/bootstrap.min.js">    </script>

  <script type="text/javascript" src="https://antimalwareprogram.co/oxygen%20/template/js/jquery.inview.min.js"></script>
  <script type="text/javascript" src="https://antimalwareprogram.co/oxygen%20/template/js/wow.min.js">    </script>
  <script type="text/javascript" src="https://antimalwareprogram.co/oxygen%20/template/js/mousescroll.js"></script>
  <script type="text/javascript" src="https://antimalwareprogram.co/oxygen%20/template/js/smoothscroll.js"> </script>
  <script type="text/javascript" src="https://antimalwareprogram.co/oxygen%20/template/js/jquery.countTo.js">    </script>
  <script type="text/javascript" src="https://antimalwareprogram.co/oxygen%20/template/js/lightbox.min.js"> </script>
  <script type="text/javascript" src="https://antimalwareprogram.co/oxygen%20/template/js/main.js"></script>
    <script src="https://cdn.ckeditor.com/4.7.2/full-all/ckeditor.js">   </script>
        <script src="https://antimalwareprogram.co/ckeditor/config.js"></script>
         <script src="https://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>

<!--Email CORRECT Response!-->
    <script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$("#f3").validate({
    rules: {
     cktext: {
        required: true,
        minlength: 10
       }
    },
    submitHandler: function () {
 console.log('successful submission! Email sent to us! Thank you for   choosing to submit a ticket to antimalwareprogram we will get back to you   asap!');
alert('successful submission! Email sent to us! Thank you for choosing to submit a ticket to antimalwareprogram we will get back to you asap!');
   }
});

//deal with copying the ckeditor text into the actual textarea
CKEDITOR.on('instanceReady', function () {
    $.each(CKEDITOR.instances, function (instance) {
    CKEDITOR.instances[instance].document.on("keyup", CK_jQ);
    CKEDITOR.instances[instance].document.on("paste", CK_jQ);
    CKEDITOR.instances[instance].document.on("keypress", CK_jQ);
    CKEDITOR.instances[instance].document.on("blur", CK_jQ);
    CKEDITOR.instances[instance].document.on("change", CK_jQ);
    });
});

function CK_jQ() {
    for (instance in CKEDITOR.instances) {
        CKEDITOR.instances[instance].updateElement();
    }
}
});//]]> 

</script>

 <?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {

  //Email information
  $admin_email = "support@antimalwareprogram.com";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $Message = $_REQUEST['cktext'];
 $ItemAddress = $_REQUEST['ItemAddress'];
      $ItemAddress2 = $_REQUEST['ItemAddress2'];
  $ItemCity = $_REQUEST['ItemCity'];
  $ItemState = $_REQUEST['ItemState'];
  $ItemZip = $_REQUEST['ItemZip'];
  $Size = $_REQUEST['Size'];
  $Location = $_REQUEST['Location'];



  $body = " Name: $name\n E-Mail: $email\n Email Body: $Message\n Location Entered: $Location\n Address: $ItemAddress\n Address Line 2: $ItemAddress2\n City: $ItemCity\n State: $ItemState\n Zip Code: $ItemZip";  
  //send 
  mail($admin_email, "$subject", $body, "From:" . $email);

  //Email FAILED response

echo "<button class=\"btn btn-info\" onclick=\"myFunction()\">Return to AntiMalwareProgram</button>\n"; 
echo "\n"; 
echo "<script>\n"; 
echo "function myFunction() {\n"; 
echo "      window.history.back();\n"; 
echo "}\n"; 
echo "</script>\n";

    echo "<script>console.log('Thank you for choosing  to submit a     support ticket to antimalwareprogram! How ever there has been a error!   Currently there is only one know cause to this issue, you must have pressed   the save button on the email body message! Please use the send button and   resubmit your ticket! We are very sorry!');</script>\n"; 
    echo "<script>alert('Error: press ok for more info!');</script>\n";


echo "\n";
echo "  <link rel=\"stylesheet\"   href=\"https://cdnjs.cloudflare.com/ajax/libs/twitter-  bootstrap/2.3.2/css/bootstrap.css\">\n"; 

 echo "  <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css \">\n"; 
echo "  <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js\"></script>\n"; 
echo "  <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script>\n"; 
    echo "  <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.css\">\n"; 

echo "<style type=\"text/css\">\n"; 
echo "    #message {\n"; 
echo "    position: fixed;\n"; 
echo "    top: 50px;\n"; 
echo "    left: 0;\n"; 
echo "    width: 100%;\n"; 
echo "}\n"; 
echo "#inner-message {\n"; 
echo "    margin: 0 auto;\n"; 
echo "}\n"; 
echo "  </style>\n"; 
echo "\n"; 
echo "<div id=\"message\">\n"; 
echo "    <div style=\"padding: 5px;\">\n"; 
echo "        <div id=\"inner-message\" class=\"alert alert-danger\">\n"; 
echo "            <strong>Error in sending of message! Details:</strong>    Thank you for choosing  to submit a  support ticket to antimalwareprogram! How ever there has been a error! Currently there is only one know cause to this issue, you must have pressed the save button on the email body message! Please use the send button and resubmit your ticket! We are very sorry!\n"; 

echo "        </div>\n"; 
echo "    </div>\n"; 
echo "</div>\n";


  }

 //if "email" variable is not filled out, display the form
  else  { 
?>

 <form method="post" name="f3" id="f3">
         <input name="subject" type="text"  class="form-control" placeholder="Email Subject" autocomplete="off" required="required"/>
   <input name="name" type="text"  class="form-control" placeholder="Name" required="required"/>
   <input type="email" name="email" class="form-control" placeholder="Email Address" required="required">





<!DOCTYPE html>
<html>
    <body>
      <label for="cktext">Email Body!</label>

 <textarea class="ckeditor" name="cktext" id="cktext" placeholder="Email   Body!"></textarea>            


        </body>
</html>

                  <button type="submit" class="btn-submit">Send!  </button>                   

  </form>

<?php
  } 
?>

我想要做的是显示一条错误消息:

 //Email FAILED response

echo "<button class=\"btn btn-info\" onclick=\"myFunction()\">Return to AntiMalwareProgram</button>\n"; 
echo "\n"; 
echo "<script>\n"; 
echo "function myFunction() {\n"; 
echo "      window.history.back();\n"; 
echo "}\n"; 
echo "</script>\n";

    echo "<script>console.log('Thank you for choosing  to submit a  support ticket to antimalwareprogram! How ever there has been a error! Currently there is only one know cause to this issue, you must have pressed the save button on the email body message! Please use the send button and resubmit your ticket! We are very sorry!');</script>\n"; 
    echo "<script>alert('Error: press ok for more info!');</script>\n";


echo "\n";
echo "  <link rel=\"stylesheet\"   href=\"https://cdnjs.cloudflare.com/ajax/libs/twitter-  bootstrap/2.3.2/css/bootstrap.css\">\n"; 

echo "  <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css       \">\n"; 
echo "  <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js\"></script>\n"; 
echo "  <script  src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"> </script>\n"; 
    echo "  <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.css\">\n"; 

echo "<style type=\"text/css\">\n"; 
echo "    #message {\n"; 
echo "    position: fixed;\n"; 
echo "    top: 50px;\n"; 
echo "    left: 0;\n"; 
echo "    width: 100%;\n"; 
echo "}\n"; 
echo "#inner-message {\n"; 
echo "    margin: 0 auto;\n"; 
echo "}\n"; 
echo "  </style>\n"; 
echo "\n"; 
echo "<div id=\"message\">\n"; 
echo "    <div style=\"padding: 5px;\">\n"; 
echo "        <div id=\"inner-message\" class=\"alert alert-danger\">\n"; 
echo "            <strong>Error in sending of message! Details:</strong> Thank you for choosing  to submit a  support ticket to antimalwareprogram! How ever there has been a error! Currently there is only one know cause to this issue, you must have pressed the save button on the email body message! Please use the send button and resubmit your ticket! We are very sorry!\n"; 

echo "        </div>\n"; 
echo "    </div>\n"; 
echo "</div>\n";

只有当我的 ckeditor 上的保存按钮被按下或电子邮件发送失败时才会出现此错误消息! 我想显示这个成功信息:

   submitHandler: function () {
console.log('successful submission! Email sent to us! Thank you for   choosing to submit a ticket to antimalwareprogram we will get back to you   asap!');
alert('successful submission! Email sent to us! Thank you for choosing to submit a ticket to antimalwareprogram we will get back to you asap!');
    }
});

这应该出现在成功发送电子邮件时,并且只有在发送按钮上才会出现,否则发送失败的响应。

这不起作用。我收到了alert('successful submission! Email sent to us! Thank you for choosing to submit a ticket to antimalwareprogram we will get back to you asap!');,但这并不意味着已发送消息。实际上这意味着它没有,我的电子邮件仅在按下 ckeditor 上的保存按钮时发送,这与我想要的相反。有没有办法解决这个问题?

【问题讨论】:

    标签: javascript php html email ckeditor


    【解决方案1】:

    我建议不要为此使用 JavaScript。改用 PHPMailer 之类的东西。

    Here's a tutorial for it.

    如果您使用的是 PHPMailer,您的代码将如下所示:

    $mail = new PHPMailer();
    $mail->setFrom("my@mail.com", 'my name');
    $mail->addAddress('your@mail.com', 'your name');
    $mail->Subject = $subject;
    $mail->Body = $message;
    $mail->isHTML(true);
    $mail->send();
    

    确保您的 $message 也包含 html 标签。如果您只想发送纯文本,只需将 isHTML 设置为 false。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多