【发布时间】:2019-06-29 09:29:28
【问题描述】:
请不要我确保我检查了之前提出的所有可能的此类问题以找到任何答案,并且看起来我遇到了障碍。我不是新开发人员。但我没有php经验。我只能做一些基本的事情。如果对 PHP 和 PHPMailer 有更多经验的人能提供帮助,我将不胜感激。
我有 3 个网站。他们大量使用联系表格。我一直在为所有人共享相同的 php 代码,并且它一直在工作,直到我昨天发现邮件程序实际上没有发送邮件。
当我检查错误日志时。这是我发现的。
[05-Feb-2019 15:41:03 Africa/Johannesburg] PHP Fatal error: Uncaught phpmailerException: Could not instantiate mail function. in /home/lovecharmking/public_html/phpmailer/class.phpmailer.php:1509
Stack trace:
#0 /home/lovecharmking/public_html/phpmailer/class.phpmailer.php(1346): PHPMailer->mailSend('Date: Tue, 5 Fe...', '<span><b>Name: ...')
#1 /home/lovecharmking/public_html/phpmailer/class.phpmailer.php(1215): PHPMailer->postSend()
#2 /home/lovecharmking/public_html/contact-form.php(47): PHPMailer->send()
#3 {main} thrown in/home/lovecharmking/public_html/phpmailer/class.phpmailer.php on line 1509.
这是我的代码。
<?php
ini_set('display_errors', '1');
require 'PHPMailerAutoload.php';
$contactEmail = $_POST['Email'];
$email2Spa = $_POST['Email2'];
$contactName = $_POST['Name'];
$contactNumber = $_POST['Cell'];
$reason = $_POST['Subject'];
$contactLocation = $_POST['Location'];
$contactMessage = $_POST['Message'];
if(!empty($email2Spa)) die();
$mail = new PHPMailer(true);
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPDebug = 2;
$mail->SMTPKeepAlive = true;
$mail->Mailer = “smtp”; // don't change the quotes!
$mail->Host = 'mail.domainname.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'help@domainname.com'; // SMTP username
$mail->Password = '**********'; // SMTP password 'TJ&ShBW[H*N#'
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('help@domainname.com', $contactName);
$mail->addAddress('help@domainname.com', 'Site Owner'); // Add a recipient
$mail->addReplyTo($contactEmail);
$mail->isHTML(true); // Set email format to HTML
$email_body = "";
$email_body .= "<span><b>Name: </b> " . $contactName . "</span><br/><br/>";
$email_body .= "<span><b>Reason: </b> " . $reason . "</span><br/><br/>";
$email_body .= "<span><b>Email Address: </b>" . $contactEmail . "</span><br/>";
$email_body .= "<p>Phone Number: " . $contactNumber . "</p>";
$email_body .= "<p><b>Location: </b>" . $contactLocation . "</p>";
$email_body .= "<p><b>Message: </b>" . $contactMessage . "</p>";
// $mail->Priority = 1;
$mail->AddCustomHeader("X-MSMail-Priority: High");
$mail->AddCustomHeader("Importance: High");
$mail->Subject = $reason;
$mail->Body = $email_body;
if(!$mail->send()) {
// echo 'Mailer Error: ' . $mail->ErrorInfo;
// $response = array('success'=>"successfully send", 'message'=>"Message sent.");;
echo json_encode(array('success' => false, "Mailer Error: ".$mail->ErrorInfo));
} else {
// return $data['success'] = true;
// echo json_encode($data);
// echo 'Message has been sent.';
echo json_encode(array('success' => true, 'message' => "You message has been sent"));
}.
这是 Ajax 调用:
$('#cform').submit(function(e){
e.preventDefault();
const formUrl = 'contact-form.php';
let formData = $('#cform').serialize(); // Collect data from form
$.ajax({
type: "POST",
url: formUrl,
data: formData,
timeout: 6000,
error: function (request, error) {
console.log(error);},
success: function (data) {
var response = JSON.parse(data);
// console.log(response);
if (response.success==true) {
let alertDiv = document.createElement('div');
alertDiv.innerHTML = `
<strong>Your Message has been Sent!</strong> Admin will get back to you as soon as he is available.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>`
$('.alert').alert();
alertDiv.setAttribute('class','alert alert-warning alert-dismissible fade show alertBox');
alertDiv.setAttribute('role','alert');
// alertDiv.setAttribute('style','display:inline-block');
let messageSpan = document.querySelector('.alertMessage');
let parentForSpan = messageSpan.parentNode;
parentForSpan.replaceChild(alertDiv,messageSpan);
// contactForm.appendChild(alertDiv);
$('#cform')[0].reset();
// alertDiv.remove()
// let span = document.createElement('span');
// span.className = "alertMessage"
// console.log(response);
} else {
console.log('Something wrong is going on. Check it.');
}
return false
}});
return false;
});
下面是 ajax 请求在浏览器中返回的内容:
Uncaught SyntaxError: Unexpected token C in JSON at position 1
at JSON.parse (<anonymous>)
at Object.success (main.js:212)
at c (jquery.min.js:4)
at Object.fireWith [as resolveWith] (jquery.min.js:4)
at k (jquery.min.js:6)
at XMLHttpRequest.r (jquery.min.js:6)
就像我说的那样,我在 3 个站点之间共享代码,此时它们都无法正常工作。他们都运行了多年。任何意见将不胜感激。
【问题讨论】:
-
我看到了这个问题
-
但是这些答案中的任何一个都没有帮助。所以我真的认为这是一个不同的问题。如果你能帮忙的话。否则不要只标记为可能重复。我不能在这里浪费人们的时间。
-
这就是为什么它被称为可能重复。我承认它没有帮助。
-
这会影响我的问题排名吗? ?你认为这是一个流行版本的问题。从昨天开始就一直想解决这个问题。问题是所有 3 个站点现在都无法正常工作。我应该切换回 neonal php 功能吗?
标签: php email phpmailer contact-form