javascript - 我无法让我的 php 邮件程序使用发布数据发送邮件
问题描述
我在使用 phpmailer 和 ajax 时遇到了一些问题。我对 ajax 很陌生,还没有完全理解它,所以这可能有一个非常明显的修复,但我们开始了。
因此,当我只是将我的 php 邮件程序脚本放在一个页面中并使用设置值访问它时,我可以发送邮件。但是每当我尝试将表单数据发送到脚本时,某些东西就不起作用。
这是我的jQuery
$(document).ready(function(){
$(".contact-form").on("submit",function(e){
e.preventDefault();
function validateEmail($email) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
return emailReg.test( $email );
}
var valid = true;
if(!$("#name").val()) {
$("#userEmail").css('border','2px solid #FFFFDF');
valid = false;
}
if( !validateEmail($(' #email').val())) {
valid = false
}
if(!$("#subject").val()) {
$("#subject").css('border','2px solid #FFFFDF');
valid = false;
}
if(!$("#message").val()) {
$("#message").css('border','2px solid #FFFFDF');
valid = false;
}
//send data to php file
var isValid;
isValid = valid
if(isValid) {
var name = $("#name").val();
var email = $("#email").val();
var subject = $("#subject").val();
var message = $("#message").val();
jQuery.ajax({
url: "includes/sendmail.inc.php",
type : "POST",
data:{name:name,email:email,subject:subject,message:message},
success:function(data){
$(".err_msg").text("Send!");
$(".err_msg").css('background-color', 'green')
$(".err_msg").show()
},
error:function (){}
});
} else {
$(".err_msg").text("Oops! check your input.");
$(".err_msg").css('background-color', 'red')
$(".err_msg").show()
}
});
});
这是我的php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
$name = $_POST['name'];
$mail = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'mail.example@example.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'example@example.com'; // SMTP username
$mail->Password = 'example123'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587 ; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom( $email , $name);
$mail->addAddress('example@example.com', 'John Doe'); // Add a recipient
$mail->addReplyTo($email , $name);
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
所以这就是我到目前为止所拥有的,我希望你们能弄清楚我做错了什么,因为我已经坚持了大约两天了。
问候弗洛里斯
解决方案
是的,我想通了,谢谢!所以出了问题的是,由于某种原因它不会发送,因为 FROM 电子邮件地址是一个 gmail 帐户,而我使用的是我自己的域 smtp,这导致了它的出现
推荐阅读
- installation - Gauss 21 Software:如何安装以下软件包?
- javascript - 为 Feather M4 Express 寻找与 usb_cdc 等效的 CircuitPython 模块
- python - pandas.DataFrame() AttributeError: 'Index' 对象没有属性 '_ndarray_values'
- json - Talend Open Studio:对 REST API 进行身份验证
- ansible - 如何确保没有手动更改 RabbitMQ 配置
- r - 在 ROUT 文件中屏蔽来自 foreach 的“启动工作人员”消息
- sql-server - 将 .csv 文件导入/加载到 SQL Server 表中
- python - 类和定义 __str__
- ruby-on-rails - 顺风没有在heroku上编译
- reactjs - 带有 React 的单选按钮