gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:20:47 29 4
gpt4 key购买 nike

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

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

这是简单的预订表格:

<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.

$mail->addBCC('outbox@handler.net'); // This is to keep a record of all emails that have been sent. Responses will also be saved to a CSV file.

$mail->Subject = 'Booking Request'; // Subject of the email sent to bookings@salon.com that the form responses will be contained within.

$mail->isHTML(TRUE);
$mail->Body = 'Booking request from '.$name.' with email '.$email; // Shows the salon the form response via the email and when they reply a new thread should start in order to compose a new email to reply to the email of the form submitter.

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(带有确认消息的过程文件)

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

<?php

use PHPMailer\PHPMailer\PHPMailer;

if(isset($_POST['submit']))
{
// Values need to be santiised

$forename = $_POST['forename'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$service = $_POST['service'];
$date = $_POST['date'];
$time = $_POST['time'];

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 = 'noreply@handler.net';
$mail->Password = '[PASSWORD]';



$mail->setFrom('Handler | Bookings'); // Emails sent via Noreply.

$mail->addAddress('bookings@salon.com'); // Email form responses sent to bookings@salon.com
$mail->addReplyTo($email); // Reply to the user who submitted the form.

$mail->addBCC('outbox@handler.net'); // Store record of all emails sent via the system.

$mail->Subject = 'Booking Request'; // Subject of the email sent to bookings@salon.com that the form responses will be contained within.

$mail->isHTML(TRUE);
$mail->Body = '

Booking request from '.$forename.' with email '.$email;'
Test Values: $forename $surname $email $phone $service $date $time



if(!$mail->send()) { // Send the email.
echo '';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo '';
}
}
?>

我不需要最后的 echo 语句,我只需要按照这种格式发送电子邮件:

<img src="https://via.placeholder.com/300x150" width="15%">
<p><b>Name:</b> $forename $surname</p>
<p><b>Email:</b> $email</p>
<p><b>Phone:</b> $phone</p>
<p><b>Service:</b> $service</p>
<p><b>Date:</b> $date</p>
<p><b>Time:</b> $time</p>

我只是不确定为什么以前发送过的电子邮件现在不会发送。任何指针将不胜感激。

更新

这是更新后的代码,感谢 Mr Perfect

<?php
mail("bookings@salon.com", "test", "message");
use PHPMailer\PHPMailer\PHPMailer;

if(isset($_POST['submit']))
{
// Values need to be santiised

$forename = $_POST['forename'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$service = $_POST['service'];
$date = $_POST['date'];
$time = $_POST['time'];

$message = <<<DELIMETER
<img src="https://via.placeholder.com/300x150" width="15%">
<p><b>Name:</b> {$forename} {$surname}</p>
<p><b>Email:</b> {$email}</p>
<p><b>Phone:</b> {$phone}</p>
<p><b>Service:</b> {$service}</p>
<p><b>Date:</b> {$date}</p>
<p><b>Time:</b> {$time}</p>
DELIMETER;

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

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'noreply@handler.com';
$mail->Password = '[PASSWORD]';

$mail->setFrom('Handler | Bookings'); // Emails sent via Noreply.

$mail->addAddress('bookings@salon.com','ADMIN'); // Email form responses sent to bookings@salon.com.

$mail->addReplyTo($email); // Reply to the user who submitted the form.

// $mail->addBCC('outbox@handler.net'); // Store record of all emails sent via the system.

$mail->Subject = 'Booking Request | SUBMISSION'; // Subject of the email sent to bookings@salon.com that the form responses will be contained within.

$mail->isHTML(TRUE);
$mail->Body = $message;
$mail->AltBody = $message;

if(!$mail->send()) { // Send the email.
echo '';
echo '' . $mail->ErrorInfo; // I don't need to echo any errors because the submission page has the text above already.
} else {
echo '';
}
}
?>

最佳答案

您应该查看您的 $mail->addAdress$mail->addBCC$mail->addReplyTo 字段并按照这些字段的正确语法。

测试下面的代码。

    <?php

use PHPMailer\PHPMailer\PHPMailer;

if(isset($_POST['submit']))
{
// Values need to be santiised

$forename = $_POST['forename'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$service = $_POST['service'];
$date = $_POST['date'];
$time = $_POST['time'];

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','Bookings'); // Emails sent via Noreply.

$mail->addAddress('bookings@salon.com',''); // Email form responses sent to bookings@salon.com

$mail->addReplyTo($email,$forename.' '.$surname); // Reply to the user who submitted the form.

$mail->addBCC('outbox@handler.net',''); // Store record of all emails sent via the system.

$mail->Subject = 'Booking Request'; // Subject of the email sent to admin@handler.net that the form responses will be contained within.

$mail->isHTML(TRUE);
$mail->Body = <<<EOD
Booking request from {$forename} with email {$email}.<br />
Contact details: <br />
Full name: {$forename} {$surname}<br />
Email: {$email} <br />
Phone number: {$phone} <br />
Service: {$service} <br />
Date: {$date} {$time}
EOD;
if(!$mail->send()) { // Send the email.
echo '';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo '';
}
}
?>

关于PHP Mailer 显示没有错误但不发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57813178/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com