【发布时间】:2020-11-18 19:00:30
【问题描述】:
朋友,请给我解释一下为什么PHPMailer不发邮件?
他必须在付款后寄一封信。通常的 mail() 函数可以工作,但是使用 PHPMailer 我已经搞砸了。帮助
付款后,此文件会收到来自 Yandex 的通知,说明付款已完成。我做了一个条件,如果付款成功了,那么相应的信件会发送到邮件中。
工作,尽管有错误
致命错误:未捕获的类型错误:传递给 YandexCheckout\Model\Notification\NotificationWaitingForCapture::__construct() 的参数 1 必须是数组类型,给定 null,在 /home/birdyxru/public_html/test/requests.php 中调用第 25 行并在 /home/birdyxru/public_html/test/assets/lib/Model/Notification/NotificationWaitingForCapture.php:72 中定义堆栈跟踪:#0 /home/birdyxru/public_html/test/requests.php(25): YandexCheckout\ Model\Notification\NotificationWaitingForCapture->__construct(NULL) #1 {main} 在第 72 行的 /home/birdyxru/public_html/test/assets/lib/Model/Notification/NotificationWaitingForCapture.php 中抛出
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
require 'assets/lib/autoload.php';
require 'db/connect.php';
// Получите данные из POST-запроса от Яндекс.Кассы
$source = file_get_contents('php://input');
$requestBody = json_decode($source, true);
// Создайте объект класса уведомлений в зависимости от события
// NotificationSucceeded, NotificationWaitingForCapture,
// NotificationCanceled, NotificationRefundSucceeded
use YandexCheckout\Model\Notification\NotificationSucceeded;
use YandexCheckout\Model\Notification\NotificationWaitingForCapture;
use YandexCheckout\Model\NotificationEventType;
use YandexCheckout\Model\PaymentStatus;
try {
$notification = ($requestBody['event'] === NotificationEventType::PAYMENT_SUCCEEDED)
? new NotificationSucceeded($requestBody)
: new NotificationWaitingForCapture($requestBody);
} catch (Exception $e) {
// Обработка ошибок при неверных данных
}
// Получите объект платежа
$payment = $notification->getObject();
if($payment->getStatus() === PaymentStatus::SUCCEEDED) {
$payment_id = $payment->getId();
}
if(isset($payment_id)) {
$email = R::getCell("SELECT `email` FROM `logs` WHERE `payment_id` = '$payment_id'");
$date = R::getCell("SELECT `date` FROM `logs` WHERE `payment_id` = '$payment_id'");
$time = R::getCell("SELECT `time` FROM `logs` WHERE `payment_id` = '$payment_id'");
$report_name = strtok("$email", '@') . ' ' . $date . ' ' . $time;
// Отправка сообщения
$mailTo = $email; // Ваш e-mail
$subject = "На сайте совершен платеж"; // Тема сообщения
// Сообщение
$message = "Платеж на сумму: " . $report_name . "<br/>";
$message .= "Детали платежа: " . $payment->description . "<br/>";
$headers= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: info@site.ru <info@site.ru>\r\n";
mail($mailTo, $subject, $message, $headers);
}
?>
不工作:(
<?php
require 'assets/lib/autoload.php';
require 'db/connect.php';
require 'mailer/src/PHPMailer.php';
// Получите данные из POST-запроса от Яндекс.Кассы
$source = file_get_contents('php://input');
$requestBody = json_decode($source, true);
// Создайте объект класса уведомлений в зависимости от события
// NotificationSucceeded, NotificationWaitingForCapture,
// NotificationCanceled, NotificationRefundSucceeded
use YandexCheckout\Model\Notification\NotificationSucceeded;
use YandexCheckout\Model\Notification\NotificationWaitingForCapture;
use YandexCheckout\Model\NotificationEventType;
use YandexCheckout\Model\PaymentStatus;
try {
$notification = ($requestBody['event'] === NotificationEventType::PAYMENT_SUCCEEDED)
? new NotificationSucceeded($requestBody)
: new NotificationWaitingForCapture($requestBody);
} catch (Exception $e) {
// Обработка ошибок при неверных данных
}
// Получите объект платежа
$payment = $notification->getObject();
if($payment->getStatus() === PaymentStatus::SUCCEEDED) {
$payment_id = $payment->getId();
}
use PHPMailer\PHPMailer\PHPMailer;
if(isset($payment_id)) {
$email = R::getCell("SELECT `email` FROM `logs` WHERE `payment_id` = '$payment_id'");
$date = R::getCell("SELECT `date` FROM `logs` WHERE `payment_id` = '$payment_id'");
$time = R::getCell("SELECT `time` FROM `logs` WHERE `payment_id` = '$payment_id'");
$report_name = strtok("$email", '@') . ' ' . $date . ' ' . $time;
$mail = new PHPMailer();
//Set who the message is to be sent from
$mail->setFrom($email_admin, $from_name);
//Set an alternative reply-to address
$mail->addReplyTo($email_admin, $from_name);
//Set who the message is to be sent to
$mail->addAddress($email);
//Set the subject line
$mail->Subject = 'PHPMailer mail() test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$message = 'Привет ' . $report_name;
$mail->msgHTML($message);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
}
?>
我猜问题出在逻辑或语法上
【问题讨论】:
-
您是否遇到任何错误?如果不是 - 你怎么确定你的邮件没有被 PHPMailer 正确发送,而不是被垃圾邮件过滤器(或其他中介)捕获?考虑到您没有在此处明确设置任何 SMTP 连接信息,后者似乎很可能,并且默认情况下,消息将通过 PHP 的内置
mail()函数 (source) 发送。这通常是leads to delivery problems。 -
通过邮件 () 发送的信件已正确发送,属于“重要”类别。我无法查看错误,因为该文件只接受 POST 并自行执行某些操作。
-
为什么接受 POST 请求会阻止您调试脚本...?你能详细说明一下吗?
-
说实话,如果浏览器中没有显示错误,我只是不知道如何查看错误。
-
调试是任何语言的核心技能——如果你不熟悉如何调试你的脚本,你真的应该重温一些fundamental documentation on the topic。特别是对于 PHP,Stack Overflow 上还有很多非常有用的资源:@987654324@、How do you debug PHP scripts?