PHP Mailer 没有显示错误但不发送电子邮件

我正在创建一个预订表格作为帮助,但已决定通过域而不是服务器发送邮件。这主要是为了更好的安全性和更少的响应和数据传输限制。


最近几天我一直在研究这个,并试图自学如何让它发挥作用。我现在有一个非常简单的工作示例,可以在下面看到。


这是简单的预订表格:


<form method="post" name="process.php" action="process.php">

<p>Name:</p><br><input type="text" name="name"><br><br>

<p>Email Address:</p><br><input type="email" name="email"><br><br>

<br>

<input type="submit" name="submit" value="Send Email">


然后在process.php我有这个工作代码:


<?php


use PHPMailer\PHPMailer\PHPMailer;


if(isset($_POST['submit'])) 

{

    // Values need to be santiised


    $name = $_POST['name']; //Name of the person requesting a booking

    $email = $_POST['email']; //Email of the person requesting a booking    


    require '../vendor/autoload.php';


    $mail = new PHPMailer;

    $mail->isSMTP();

    $mail->SMTPDebug = 0;

    $mail->Host = 'smtp.hostinger.com';

    $mail->Port = 587;

    $mail->SMTPAuth = true;

    $mail->Username = 'test@handler.net';

    $mail->Password = '[PASSWORD]';




    $mail->setFrom('test@handler.net'); // All emails would be sent from the handler.net domain to the bookings email of the other domain. (Using test to test, will be noreply)


    $mail->addAddress('bookings@salon.com'); // Recipient of the email should be the bookings address, this won't change.


    $mail->addReplyTo($email); // The reply to address will be the email address of the user who submitted the booking enquiry.


    if(!$mail->send()) { // Send the email.

      echo 'Message was not sent.';

      echo 'Mailer error: ' . $mail->ErrorInfo;

    } else {

      echo 'Message has been sent.';

    }

}

?>

上面的代码有效并将电子邮件从正确的地址发送到正确的地址。那是在一个站点上public_html/testing并且已被移动到另一个站点中,public_html/booking因此相对路径将是相同的。此目录中的唯一文件是index.php(表单)和send.php(带有确认消息的流程文件)


出于某种原因,这个包含所有表单值的新代码将不会发送。老实说,我不太确定为什么它现在不起作用,因此将非常感谢任何指针。


手掌心
浏览 209回答 3
3回答

HUX布斯

您应该查看$mail->addAdress,$mail->addBCC和$mail->addReplyTo字段并遵循这些字段的正确语法。测试下面的代码。&nbsp; &nbsp; <?php&nbsp; &nbsp; use PHPMailer\PHPMailer\PHPMailer;&nbsp; &nbsp; if(isset($_POST['submit']))&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Values need to be santiised&nbsp; &nbsp; &nbsp; &nbsp; $forename = $_POST['forename'];&nbsp; &nbsp; &nbsp; &nbsp; $surname = $_POST['surname'];&nbsp; &nbsp; &nbsp; &nbsp; $email = $_POST['email'];&nbsp; &nbsp; &nbsp; &nbsp; $phone = $_POST['phone'];&nbsp; &nbsp; &nbsp; &nbsp; $service = $_POST['service'];&nbsp; &nbsp; &nbsp; &nbsp; $date = $_POST['date'];&nbsp; &nbsp; &nbsp; &nbsp; $time = $_POST['time'];&nbsp; &nbsp; &nbsp; &nbsp; require '../vendor/autoload.php';&nbsp; &nbsp; &nbsp; &nbsp; $mail = new PHPMailer;&nbsp; &nbsp; &nbsp; &nbsp; $mail->isSMTP();&nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPDebug = 0;&nbsp; &nbsp; &nbsp; &nbsp; $mail->Host = 'smtp.hostinger.com';&nbsp; &nbsp; &nbsp; &nbsp; $mail->Port = 587;&nbsp; &nbsp; &nbsp; &nbsp; $mail->SMTPAuth = true;&nbsp; &nbsp; &nbsp; &nbsp; $mail->Username = 'test@handler.net';&nbsp; &nbsp; &nbsp; &nbsp; $mail->Password = '[PASSWORD]';&nbsp; &nbsp; &nbsp; &nbsp; $mail->setFrom('test@handler.net','Bookings'); // Emails sent via Noreply.&nbsp; &nbsp; &nbsp; &nbsp; $mail->addAddress('bookings@salon.com',''); // Email form responses sent to bookings@salon.com&nbsp; &nbsp; &nbsp; &nbsp; $mail->addReplyTo($email,$forename.' '.$surname); // Reply to the user who submitted the form.&nbsp; &nbsp; &nbsp; &nbsp; $mail->addBCC('outbox@handler.net',''); // Store record of all emails sent via the system.&nbsp; &nbsp; &nbsp; &nbsp; $mail->Subject = 'Booking Request'; // Subject of the email sent to admin@handler.net that the form responses will be contained within.&nbsp; &nbsp; &nbsp; &nbsp; $mail->isHTML(TRUE);&nbsp; &nbsp; &nbsp; &nbsp; $mail->Body = <<<EOD&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Booking request from {$forename} with email {$email}.<br&nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Contact details: <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Full name: {$forename} {$surname}<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Email: {$email} <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Phone number: {$phone} <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Service: {$service} <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date: {$date} {$time}EOD;&nbsp; &nbsp; &nbsp; &nbsp; if(!$mail->send()) { // Send the email.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'Mailer error: ' . $mail->ErrorInfo;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; ?>

海绵宝宝撒

这段代码是错误的,缺少一些引号$mail->Body = '&nbsp; &nbsp; Booking request from '.$forename.' with email '.$email;'&nbsp; &nbsp; Test Values: $forename $surname $email $phone $service $date $time要在字符串上启用变量替换,请使用双引号,并且您不需要使用 dot(.) 连接变量,这也可以使用像 \n 这样的转义字符,尝试像这样调整您的代码:$mail->Body = "Booking request from $forename with email $email\n" .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Test Values: $forename $surname $email $phone $service $date $time";

侃侃无极

首先,我们必须查看错误,因此您必须设置$mail->SMTPDebug&nbsp;=&nbsp;0;进入$mail->SMTPDebug&nbsp;=&nbsp;3;因此,您可以根据该错误发布该错误。
打开App,查看更多内容
随时随地看视频慕课网APP