我正在学习php,对于一个项目,我正在尝试使用phpmailer。我托管了一个网站,正在尝试通过联系表格发送邮件,但它似乎无法正常工作。
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if(isset($_POST['submit'])) {
//Load Composer's autoloader
require 'vendor/autoload.php';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'example.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom($email, $name);
$mail->addAddress('test@mail.com', 'John Doe'); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Mail from Website';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'Testing mail';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
header("Location: index.php");
我已更改了一些信息,尤其是出于安全原因,尤其是在SMTP中。
所以我做了一些更改,并将其作为输出
2018-10-10 06:42:52 SERVER -> CLIENT: 220-cp-ht-9.webhostbox.net ESMTP Exim 4.91 #1 Wed, 10 Oct 2018 06:42:52 +0000 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2018-10-10 06:42:52 CLIENT -> SERVER: EHLO www.hostname.com
2018-10-10 06:42:52 SERVER -> CLIENT: 250-cp-ht-9.webhostbox.net Hello www.hostname.com [162.241.148.100]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
2018-10-10 06:42:52 CLIENT -> SERVER: STARTTLS
2018-10-10 06:42:52 SERVER -> CLIENT: 220 TLS go ahead
2018-10-10 06:42:52 CLIENT -> SERVER: EHLO www.christopherdias.info
2018-10-10 06:42:52 SERVER -> CLIENT: 250-cp-ht-9.webhostbox.net Hello www.hostname.com [162.241.148.100]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250 HELP
2018-10-10 06:42:52 CLIENT -> SERVER: AUTH LOGIN
2018-10-10 06:42:52 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2018-10-10 06:42:52 CLIENT -> SERVER: <credentials hidden>
2018-10-10 06:42:52 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2018-10-10 06:42:52 CLIENT -> SERVER: <credentials hidden>
2018-10-10 06:42:52 SERVER -> CLIENT: 235 Authentication succeeded
2018-10-10 06:42:52 CLIENT -> SERVER: MAIL FROM:<test@mail.com>
2018-10-10 06:42:52 SERVER -> CLIENT: 250 OK
2018-10-10 06:42:52 CLIENT -> SERVER: RCPT TO:<me@hostname.com>
2018-10-10 06:42:52 SERVER -> CLIENT: 250 Accepted
2018-10-10 06:42:52 CLIENT -> SERVER: DATA
2018-10-10 06:42:52 SERVER -> CLIENT: 354 Enter message, ending with "." on a line by itself
2018-10-10 06:42:52 CLIENT -> SERVER: Date: Wed, 10 Oct 2018 06:42:52 +0000
2018-10-10 06:42:52 CLIENT -> SERVER: To: Jane Doe <me@hostname.com>
2018-10-10 06:42:52 CLIENT -> SERVER: From: John <test@mail.com>
2018-10-10 06:42:52 CLIENT -> SERVER: Subject: Mail from Website
2018-10-10 06:42:52 CLIENT -> SERVER: Message-ID: <XHLR5jcAlBhaDRm37Jtfwnnz5njYjR0UViZapBxdG4Y@www.hostname.com>
2018-10-10 06:42:52 CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
2018-10-10 06:42:52 CLIENT -> SERVER: MIME-Version: 1.0
2018-10-10 06:42:52 CLIENT -> SERVER: Content-Type: multipart/alternative;
2018-10-10 06:42:52 CLIENT -> SERVER: boundary="b1_XHLR5jcAlBhaDRm37Jtfwnnz5njYjR0UViZapBxdG4Y"
2018-10-10 06:42:52 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2018-10-10 06:42:52 CLIENT -> SERVER:
2018-10-10 06:42:52 CLIENT -> SERVER: This is a multi-part message in MIME format.
2018-10-10 06:42:52 CLIENT -> SERVER: --b1_XHLR5jcAlBhaDRm37Jtfwnnz5njYjR0UViZapBxdG4Y
2018-10-10 06:42:52 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2018-10-10 06:42:52 CLIENT -> SERVER:
2018-10-10 06:42:52 CLIENT -> SERVER: Testing mail
2018-10-10 06:42:52 CLIENT -> SERVER:
2018-10-10 06:42:52 CLIENT -> SERVER: --b1_XHLR5jcAlBhaDRm37Jtfwnnz5njYjR0UViZapBxdG4Y
2018-10-10 06:42:52 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2018-10-10 06:42:52 CLIENT -> SERVER:
2018-10-10 06:42:52 CLIENT -> SERVER: This is the HTML message body <b>in bold!</b>
2018-10-10 06:42:52 CLIENT -> SERVER:
2018-10-10 06:42:52 CLIENT -> SERVER:
2018-10-10 06:42:52 CLIENT -> SERVER: --b1_XHLR5jcAlBhaDRm37Jtfwnnz5njYjR0UViZapBxdG4Y--
2018-10-10 06:42:52 CLIENT -> SERVER:
2018-10-10 06:42:52 CLIENT -> SERVER: .
2018-10-10 06:42:52 SERVER -> CLIENT: 250 Message denied for spoofing attempt via SMTP Auth (From address: John <test@mail.com> IP: 162.241.148.100 AuthenticatedID: me@hostname.com Account owner: myname)
2018-10-10 06:42:52 CLIENT -> SERVER: QUIT
2018-10-10 06:42:52 SERVER -> CLIENT: 221 cp-ht-9.webhostbox.net closing connection
Message has been sent
邮件仍然没有发送。
答案 0 :(得分:0)
在您的代码中,您有SMTPSecure = 'tls'
,但有Port = 465
。这将行不通,所有示例都没有,文档告诉您这种组合将行不通。您的ISP也不会告诉您执行此操作。通过这种组合,您将无法获得调试输出,因为它根本无法成功打开连接,因此没有SMTP流量可供调试。
将其更改为SMTPSecure = 'ssl'
或(不是和)Port = 587
。