PHPMailer 未在 Windows10 上与 Xampp 一起运行 [英] PHPMailer not running with Xampp on Windows10
问题描述
我的问题是当我使用 Xampp 和 Windows10 运行 PHPMailer(准确地说是 PHPMailer-master 6.0.3)时,它无法发送电子邮件.(我发现了很多关于该主题的评论,但都没有找到解决方案.)
my problem is that PHPMailer (PHPMailer-master 6.0.3 to be exact) does not deliver emails when I run it with Xampp and Windows10. (I found a lot of comments on that subject but none of them led to a solution.)
以下代码在远程服务器上运行良好:
The following code runs fine on a remote server:
<?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;
// 'PHPMailer' here actually is the original folder 'PHPMailer-master'
// from unpacking the downloaded file PHPMailer-master.zip
require 'vendor/PHPMailer/src/Exception.php';
require 'vendor/PHPMailer/src/PHPMailer.php';
require 'vendor/PHPMailer/src/SMTP.php';
echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n";
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->$mail->isSendmail(); // corrected
$mail->Host = 'smtp.kabelmail.de'; //smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myname@kabelmail.de'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('myname@kabelmail.de', 'myname'); // Add a recipient
// $mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('myname@web.de', 'Antwort');
//$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 = 'Here is the subject:localhost';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = ' body in plain text for non-HTML mail lients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>
我保留了上面的脚本,并根据 Phpmailer 无法从本地主机 (XAMPP) 运行:
I left the script above as it is and modified php.ini for Xampp in accordance with the comments at Phpmailer not working running from localhost (XAMPP):
[mail function]
SMTP=smtp.kabelmail.de
smtp_port=465
sendmail_from = to@kabelmail.de
sendmail_path ="C:\xampp\sendmail\sendmail.exe\"
;(I also tried sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" but without success.)
mail.log="C:\xampp\php\logs\php_mail.log"
这些是对 sendmail.ini 的修改:
These are the modifications to sendmail.ini:
[sendmail]
smtp_server=smtp.kabelmail.de
smtp_port=465
smtp_ssl=auto
error_logfile=error.log
debug_logfile=debug.log
auth_username=myname@kabelmail.de
auth_password=mypassword
结果:1. 通过上面的设置,我收到了这条消息:
Results: 1. With the settings above I got this message:
SSL loaded 2018-01-11 12:06:10 SERVER -> CLIENT: 421 4.3.2 Too many open connections.
2018-01-11 12:06:10 CLIENT -> SERVER: EHLO localhost
2018-01-11 12:06:10 SERVER -> CLIENT:
2018-01-11 12:06:10 SMTP ERROR: EHLO command failed:
2018-01-11 12:06:10 SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not connect to SMTP host.
SMTP Error: Could not connect to SMTP host.
Message could not be sent.Mailer Error: SMTP Error: Could not connect to SMTP host.
- 然后我替换了 $mail->isSendmail();通过 $mail->isMail();现在出现的消息是已加载 SSL 的消息已发送
这就是我要找的,但是 - 邮箱中没有消息.!!!php_mail.log 有这个信息,对我来说并不可疑:
That is what I was looking for, but - there was no message in the mailbox.!!! php_mail.log had this information, which doesn't look suspicious to me:
[11-Jan-2018 13:09:32 Europe/Berlin] mail() on [C:\xampp\htdocs\to\vendor\PHPMailer\src\PHPMailer.php:768]: To: "name" <myname@kabelmail.de> -- Headers: Date: Thu, 11 Jan 2018 13:09:32 +0100 From: Mailer <from@example.com> Reply-To: Antwort <myname@web.de> Message-ID: <VuAQ3BR022MQyNd3hKCoguqr50Ry9TPG4vIRL2ZmFg@localhost> X-Mailer: PHPMailer 6.0.3 (https://github.com/PHPMailer/PHPMailer) MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="b1_VuAQ3BR022MQyNd3hKCoguqr50Ry9TPG4vIRL2ZmFg" Content-Transfer-Encoding: 8bit
有人可以给我一个提示可能是什么问题吗?我已经为此工作了好几天,但显然我缺少一些基本的东西.
Can somebody give me a hint what might be wrong? I have been working on that for several days now but obviously I am missing something basic.
--- 编辑 2018 年 1 月 12 日 --------------------------------
--- Edit Jan. 12, 2018 -------------------------------------------------
$mail->isSendmail();是远程服务器上的设置没问题!
$mail->isSendmail(); is the setting that is ok on the remote server!
推荐答案
已解决.
当我搬到 smtp.web.de 时取得了突破.
The breakthrough was reached when I moved to smtp.web.de.
我现在从客户端和服务器获取消息 ($mail->SMTPDebug = 2;).
I now get the the messages from client and server ($mail->SMTPDebug = 2;).
服务器仍然抱怨
$mail->setFrom('from@example.com', 'Mailer');
说
MAIL FROM 命令失败:550-未采取请求的操作:邮箱不可用 550 发件人地址不被允许".
"MAIL FROM command failed: 550-Requested action not taken: mailbox unavailable550 Sender address is not allowed".
替换为
$mail->setFrom('myname@web.de', 'via web.de');
完成任务.但并非所有服务器都抱怨这一点.例如 Dogado.de 就没有.
did the job. But not all servers complain about that. Dogado.de for instance does not.
最后:
$mail->SMTPDebug = 0; // suppresses server and client messages for production use
$mail->CharSet = "UTF-8"; // for correct umlauts
总结:
以下代码可用于本地机器(Xampp、Netbeans)以及远程服务器.
The following code can be used on a local machine (Xampp, Netbeans) as well as on a remote server.
<?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;
// adjust path accordingly!
require 'vendor/PHPMailer/src/Exception.php';
require 'vendor/PHPMailer/src/PHPMailer.php';
require 'vendor/PHPMailer/src/SMTP.php';
// is ssl loaded? (test only):
//echo (extension_loaded('openssl')?'SSL loaded, ':'SSL not loaded, ')."\n";
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
$mail->SMTPDebug = 0; // production use
$mail->isSMTP(); // Set mailer to use SMTP
//=== using web.de ========================================
// adjust settings to your project!
$mail->Host = 'smtp.web.de'; //smtp1.example.com;smtp2.example.com';
// Specify main and backup SMTP servers
$mail->Username = 'myname@web.de'; // SMTP username
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('myname@web.de', 'über web.de'); // required by web.de
$mail->Password = 'mypassword'; // SMTP password
//==========================================================
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
//Recipients
$mail->addAddress('myname@kabelmail.de', 'my name'); // Add a recipient
$mail->addAddress('myname@web.de'); // Name is optional
$mail->CharSet = "UTF-8"; // because of umlauts
//$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 = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold! groß süß ähnlich Ökonom</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients: groß süß ähnlich Ökonom';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
这篇关于PHPMailer 未在 Windows10 上与 Xampp 一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!