Я изучаю 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
Почта все еще не отправляется.