php - 使用 PHPMailer 发送表单信息
问题描述
我创建了一个表单,我想使用 PHPMailer 邮寄表单数据。我能做些什么?
我的contact.html文件:
<script src="../../Scripts/Custom/contact.js"></script>
<div class="contact-container">
<div class="form-container">
<p class="contactus-heading">Contact us</p>
<div class="inner-form-container">
<div class=" form-row "><input type="text" id="name" class="contact-form-text" placeholder="Your Name here" name="name" /></div>
<div class="form-row"><input type="email" id="email" class="contact-form-text" placeholder="Email" name="email" /></div>
<div class=" form-row"><input type="text" id="phone" class="contact-form-text" placeholder="Phone Number" name="phone" /></div>
<div class="form-row"><textarea class="contact-form-text" id="body" placeholder="your message here" name="message"></textarea></div>
<div class="form-row buttons">
<input type="submit" onclick="sendEmail()" name="btn-send" class="send-btn contact-form-btn" value="send" />
</div>
</div>
</div>
</div>
我的 Contact.js 文件:
function sendEmail() {
var name = $('#name')[0].value;
var email = $('#email')[0].value;
var phone = $('#phone')[0].value;
var body = $('#body')[0].value;
if (isNotEmpty(name, email, phone, body)) {
debugger;
$.ajax({
url: '../../Pages/Shared/mail.php',
type: 'POST',
name: name,
email: email,
phone: phone,
body: body,
success: function (response) {
console.log(response);
}, error: function (e) {
console.log(e);
}
});
}
}
function isNotEmpty(name, email, phone, body) {
debugger
if (name != "" && email != "" && phone != "" && body != "") {
return true;
} else {
console.log('some fields are empty');
return false
}
}
我的 mail.php 文件:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if(isset($_POST['name']) && isset($_POST['email'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$body=$_POST['body'];
require_once "../../Libraries/PHPMailer.php"
require_once "../../Libraries/SMTP.php"
require_once "../../Libraries/Exception.php"
$mail = new PHPMailer();
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mailhere'; // SMTP username
$mail->Password = 'password here'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587;
$mail->isHTML(true);
$mail->setFrom($email, $name);
$mail->addAddress('mailhere'); // Add a recipient
$mail->Subject = 'It is a subject';
$mail->addReplyTo('mailhere');
$mail->Body = $body;
if($mail->send())
$response="Email is sent!";
else
$response= "Something went wrong :<br><br>".$mail->ErrorInfo;
exit(json_encode(array("response" =>$response)));
}
?>
当我在表格中填写信息时,我得到的错误是:
发送到 Web 服务器的请求使用了配置为处理请求的模块所不允许的 HTTP 谓词。\t
解决方案
推荐阅读
- haskell - Haskell Stack 显示错误“找不到包 unix 的包 ID”。如何修复此错误?
- ios - Obj-C - 在特定时间触发本地通知?
- node.js - Github API 获取 repo 的自述文件,不管它是如何命名的
- python - os.mkdir 成功但没有目录出现在 C:\Users\
- sql - SQL:显示计算字段为最大值的记录
- sql - 带有序列号的 SQL 分组依据
- php - 带有奇怪的隐藏输入字段的 HTML 表单渲染
- python - 使用 Python Socket 分块发送和接收文件
- jenkins - 如何将数字参数作为手动输入输入到我的 Jenkins 声明性管道中?
- javascript - React 登录页面在加载页面而不是“用户名”时专注于“密码”