【发布时间】:2018-02-26 20:21:00
【问题描述】:
填写联系我们表格后,我想通过用户向 domain@host.com 发送邮件。 在这两种情况下成功发送或发送失败消息都显示在新页面中,我想在提交后在我的联系我们页面上显示这些消息。
html
<form id="form"class="form" action="sendEmail.php" method="POST">
<p class="name">
<input type="text" name="name" id="name" placeholder="John Doe" />
<label for="name">Name</label>
</p>
<p class="email">
<input type="text" name="email" id="email" placeholder="mail@example.com" />
<label for="email">Email</label>
</p>
<p class="subject">
<input type="text" name="subject" id="subject" placeholder="www.example.com" />
<label for="subject">subject</label>
</p>
<p class="text">
<textarea name="message" id="comment" placeholder="Write something to us" /></textarea>
</p>
<div id="complete" class="complete hide">
<h3>Thank you</h3>
<p>We will be in contact with you soon!</p>
</div>
<p class="submit">
<input type="submit" value="Send" />
</p>
</form>
这是我的 sendEmail.php 代码
<?php
$toAddress = 'domain@host.com'; //eamil will be sent to this account...
$EmailSentBy= $_POST['email'];
$userEmail = $_POST['name'];
$title = $_POST['subject'];
$message = $_POST['message'];
//****************************************************************
require 'PHPMailer-5.2.23/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail -> isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail -> SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail -> Debugoutput = 'html';
//Set the hostname of the mail server
$mail -> Host = 'smtp.gmail.com';///
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail -> Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail -> SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail -> SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail -> Username = "domain@host.com";//email address from which it is send to other //
//Password to use for SMTP authentication
$mail -> Password = "ooovxxazwdbxmcgn";//password of website email account
//Set who the message is to be sent from
$mail -> setFrom($toAddress, "Contact Us Page");
//Set an alternative reply-to address
$mail -> addReplyTo($userEmail, "UserName here");
//Set who the message is to be sent to
$mail -> addAddress($toAddress, "Mr/Mrs.");//receiver
//Set the subject line
$mail -> Subject = $title;
$msg = 'Message';
//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail -> msgHTML($msg);
//Replace the plain text body with one created manually
$mail -> AltBody = 'This is an auto generated email from oric.uob.edu.pk';
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//************************************************************************
if (!$mail -> send()) {
echo json_encode(array('status' => false, 'msg' => 'There was a problem while sending Email' . $mailer -> ErrorInfo));
die();
}else{
echo json_encode(array('status' => TRUE, 'msg' => 'Successfully Approved'));
die();
}
?>
Jscript
$(function() {
var form = $('#form');
$(form).submit(function(event) {
event.preventDefault();
var formData = $(form).serialize();
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
response=$("#msg").html("<i>Thank you </i>")
})
.fail(function(data) {
});
})
});
我无法找出问题所在,非常感谢任何形式的帮助。 提前致谢。
【问题讨论】:
-
你在开玩笑吗?如果你甚至不在 Jscript 中调用它们,你应该如何实现你想要的。
-
您需要提供minimal reproducible example。您的问题中缺少包含您的表单的页面的 HTML。 PHP 在很大程度上是无关紧要的(至少除非你已经调试了足够多的东西以确定你从 PHP 得到错误的响应)。
-
我收到了响应但它重定向到其他页面并在那里显示响应数据
-
开发者工具的控制台有报错吗? (确保您已将其配置为在页面加载之间保留日志)。
-
@naizoric — 发表评论时,没有
<form>可附加到该事件。