- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试将 phpmailer 与允许您发送表单而无需刷新页面的功能相结合。通常一切看起来都很好,只是电子邮件没有发送。在有关 phpmailer 的清晰代码上,一切正常。
我希望无需刷新页面即可发送电子邮件。也许有人遇到过类似的问题?
index.html
<form name="ContactForm" method="post" action="">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name">
</div>
<div class="form-group">
<label for="email">Email Address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea name="message" class="form-control" id="message"></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<div class="message_box" style="margin:10px 0px;">
</div>
<script>
$(document).ready(function() {
var delay = 2000;
$('.btn-default').click(function(e){
e.preventDefault();
var name = $('#name').val();
if(name == ''){
$('.message_box').html(
'<span style="color:red;">Enter Your Name!</span>'
);
$('#name').focus();
return false;
}
var email = $('#email').val();
if(email == ''){
$('.message_box').html(
'<span style="color:red;">Enter Email Address!</span>'
);
$('#email').focus();
return false;
}
if( $("#email").val()!='' ){
if( !isValidEmailAddress( $("#email").val() ) ){
$('.message_box').html(
'<span style="color:red;">Provided email address is incorrect!</span>'
);
$('#email').focus();
return false;
}
}
var message = $('#message').val();
if(message == ''){
$('.message_box').html(
'<span style="color:red;">Enter Your Message Here!</span>'
);
$('#message').focus();
return false;
}
$.ajax({
type: "POST",
url: "ajax.php",
data: "name="+name+"&email="+email+"&message="+message,
beforeSend: function() {
$('.message_box').html(
'<img src="Loader.gif" width="25" height="25"/>'
);
},
success: function(data)
{
setTimeout(function() {
$('.message_box').html(data);
}, delay);
}
});
});
});
</script>
ajax.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// if(isset($_POST['submit'])){
// $name= $_POST['name'];
// $email= $_POST['email'];
// $tel= $_POST['tel'];
// $message= $_POST['message'];
if ( ($_POST['name']!="") && ($_POST['email']!="")){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.mailtrap.io'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'bf4908e76c4186'; // SMTP username
$mail->Password = 'fe1e3963078670'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom($email, $name);
$mail->addAddress('pawel@gmail.com', 'Joe User'); // Add a recipient
$mail->addAddress('pawel@gmail.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = $message;
$mail->send();
if($send){
echo "<span style='color:green; font-weight:bold;'>
Thank you for contacting us, we will get back to you shortly.
</span>";
}
else{
echo "<span style='color:red; font-weight:bold;'>
Sorry! Your form submission is failed.
</span>";
}
}
向我显示副标题:“感谢您联系我们,我们会尽快回复您。”但不幸的是邮箱不是空的。
最佳答案
除了 try/catch 问题,您还有其他问题。
SMTPSecure = 'tls'
和 Port = 465
的组合将不起作用;要么更改为 ssl
模式,要么更改 Port = 587
。这在故障排除指南中有详细记录。
不要使用提交者的地址作为发件人地址;这是伪造的,会导致您的邮件因 SPF 失败而被退回或被垃圾邮件过滤。将您自己的地址放在表单地址中,并将提交者的地址放在回复中 - 请参阅 PHPMailer 提供的联系表单示例。
关于php - 使用 phpmailer 发送表单时没有页面刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316962/
如何安装 phpMailer在共享托管环境中?我需要将其用于用户的电子邮件验证和密码更改。 最佳答案 您可以在这里下载:https://github.com/PHPMailer/PHPMailer 将
当我使用 phpmailer 通过 Gmail 发送时,电子邮件需要很长时间才能发送,而且有时发送不成功。我正在使用 XAMPP 作为本地主机。 这是我的代码: if($_POST['submit']
我正在使用 PHPMailer 发送一些简单的电子邮件,但是函数 SetFrom() 似乎不起作用,即使我使用的代码直接来自 phpmails 文档 ( http://phpmailer.worxwa
使用 PHP 邮件程序 6.0.6 版 我知道类似的问题得到了回答,但我的情况不同。 我已经成功配置了它运行良好的脚本;我正在接收邮件,然后突然之间没有任何变化,它开始向我抛出这个错误。 错误
我可以生成 pdf 以便轻松查看和下载。我正在尝试发送一封附有 pdf 的电子邮件。我已经连续几天浏览谷歌了几个小时,没有什么比这个链接更让我更接近了,sending an email attachm
我可以生成 pdf 以便轻松查看和下载。我正在尝试发送一封附有 pdf 的电子邮件。我已经连续几天浏览谷歌了几个小时,没有什么比这个链接更让我更接近了,sending an email attachm
由于错误,我无法收到确认电子邮件。这是我的 PhpMailer 错误。 fatal error :未捕获错误:在 C:\xampp\htdocs\Web\PHPMailer\PHPMailer.php
这个问题在这里已经有了答案: SMTP connect() failed PHPmailer - PHP (17 个答案) 关闭 7 年前。 这是我在网上引用了很多之后从本地主机发送电子邮件的代码。
我写了一个 PHP 脚本,可以发送邮件。我从“mail@something.com”发送它们并将“返回路径”设置为“bounce@something.com”,但我仍然收到退回邮件到发件人邮件(“ma
我使用 php curl() 函数从服务器 A 连接到邮件服务器 B 以发送邮件。 $body = "body\n\nmore body\nmore body\n\nmore body\n\n"; $
PHPMailer 只是在发送电子邮件时挂起。我逐行注释了所有代码,一切正常,直到到达 如果(!$邮件->发送()) 此时页面进入旋转状态 - 没有错误消息或任何其他错误指示。我想我拥有所有必需的
我在问这里之前阅读了一些其他问题,因为其他答案没有回应我的问题。 我有一个用 php 定制的 cms。例如,如果我插入一个新的付款,脚本会向所有管理员用户发送一个通知: function insert
我用 Ubuntu 和 DigitalOcean 构建了一个 droplet,我正在尝试将它配置为使用 SMTP 发送电子邮件。 我知道 DigitalOcean 通过 IPv6 而不是通过 IPv4
我是 PHP 的新手。我从这里下载了一个 phpmailer 脚本(带有文件)-> phpmailer 我正在使用 XAMPP,我将文件定位在: \htdocs\QMS\phpmailer 我的 ph
我这里有一个大问题! 我需要向所有订阅者发送时事通讯(大约 1200) 问题是它只将时事通讯发送给其中的 150-180。 我有一个在 php 中实现的脚本,它使用 PhpMailer() 类将时事通
是否可以使用phpmailer发送异步电子邮件? 常规邮件发送代码段如下: $mail->Send(); PHP在继续之前等待Send()返回结果。是否有可能使phpmailer立即返回结果,而无需等
我有这样一种情况,我发送了两封不同的电子邮件,一封给客户,另一封在成员接受任务后发送给成员。在函数中我调用了 2 个函数,一个发送给客户端,一个发送给成员。发生的情况是第二封电子邮件同时发送给成员(m
我正在使用 PHPMailer在我的网站上,但它返回一个错误: 您必须提供至少一个收件人电子邮件地址。 服务器正在运行 PHP 7。我查看了以下页面以寻找答案: PHP Mailer You must
我正在尝试配置 PHPMailer 我上传了 1 个文件,它是 class.phpmailer.php 并创建了另一个包含以下内容的 php 文件: IsSMTP(); // enable SMTP
使用 PHPMailer 向我的 Gmail 帐户发送电子邮件后,单击“邮件发送者”右侧的“显示详细信息”后,它会显示“yourhostingaccount.com”。这是一张图片: 我读过您可以使用
我是一名优秀的程序员,十分优秀!