php Swift Mailer - 无法发送邮件,也找不到错误日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19366289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift Mailer - Can't send mail, and can't find error logs
提问by d.h276
New to PHP and Swiftmailerand have yet to get it working. I've uploaded the /lib/ directory to a directory in the root of my shared webserver from Hostgator. I've uploaded the following inside in the directory above /lib/:
PHP 和Swiftmailer 的新手,还没有让它工作。我已将 /lib/ 目录从 Hostgator 上传到我的共享网络服务器的根目录中。我已经将以下内容上传到 /lib/ 上面的目录中:
<?php
require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('mail.****.com', 25)
->setUsername('****@****.com')
->setPassword('****');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Subject Here')
->setFrom(array('****@****.com' => '****'))
->setTo(array('****@****.com' => '****'));
$message->setBody('This is the message');
if (!$mailer->send($message, $errors))
{
echo "Error:";
print_r($errors);
}
?>
It does not send a message, but I am also unable to view any error logs. I have error logging enabled in all of sections in my php.ini - but when I try to go to where I uploaded the .php file in a browser I get a 404 error. When I connect through ssh I have jailshell access. When I tried to go to /var/log/php-scripts.log I did not have permission. Wondering where else I could find errors for this in order to fix it?
它不发送消息,但我也无法查看任何错误日志。我在 php.ini 的所有部分都启用了错误日志记录 - 但是当我尝试转到在浏览器中上传 .php 文件的位置时,出现 404 错误。当我通过 ssh 连接时,我可以访问越狱。当我尝试访问 /var/log/php-scripts.log 时,我没有获得许可。想知道我还能在哪里找到错误以修复它?
回答by cyb0k
You should try to use swiftmailer logger plugin.
您应该尝试使用 swiftmailer logger plugin。
The Logger plugins helps with debugging during the process of sending. It can help to identify why an SMTP server is rejecting addresses, or any other hard-to-find problems that may arise.
Logger 插件有助于在发送过程中进行调试。它可以帮助确定 SMTP 服务器拒绝地址的原因,或可能出现的任何其他难以发现的问题。
You only need to create new logger instance and add it to mailer object with registerPlugin() method.
您只需要创建新的记录器实例并使用 registerPlugin() 方法将其添加到邮件程序对象。
<?php
require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('mail.****.com', 25)
->setUsername('****@****.com')
->setPassword('****');
$mailer = Swift_Mailer::newInstance($transport);
$logger = new \Swift_Plugins_Loggers_ArrayLogger();
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
$message = Swift_Message::newInstance('Subject Here')
->setFrom(array('****@****.com' => '****'))
->setTo(array('****@****.com' => '****'));
$message->setBody('This is the message');
if (!$mailer->send($message, $errors)) {
// Dump the log contents
// NOTE: The EchoLogger dumps in realtime so dump() does nothing for it. We use ArrayLogger instead.
echo "Error:" . $logger->dump();
}else{
echo "Successfull.";
}
?>
Edit :
编辑 :
Swift_Plugins_Loggers_ArrayLogger: Keeps a collection of log messages inside an array. The array content can be cleared or dumped out to the screen.
Swift_Plugins_Loggers_EchoLogger: Prints output to the screen in realtime. Handy for very rudimentary debug output.
Swift_Plugins_Loggers_ArrayLogger:在数组中保存日志消息的集合。阵列内容可以被清除或转储到屏幕上。
Swift_Plugins_Loggers_EchoLogger:实时将输出打印到屏幕上。对于非常基本的调试输出非常方便。
回答by kjdion84
Use transport exception handling like so:
像这样使用传输异常处理:
try {
$mailer->send($message);
}
catch (\Swift_TransportException $e) {
echo $e->getMessage();
}
回答by Jeff Davis
Under your swift mailer extension there is an exception handler.
在您的 swift 邮件程序扩展下,有一个异常处理程序。
root/lib/classes/Swift/SwiftException.php
root/lib/classes/Swift/SwiftException.php
By default it does some convoluted things with errors to make it really hard to find them. Most of the pages out there also recommend equally convoluted ways of logging the errors.
默认情况下,它会执行一些带有错误的令人费解的事情,因此很难找到它们。那里的大多数页面还推荐了同样复杂的记录错误的方法。
If you just want to see the error, add an echo, like below (or whatever else you might want to do with it).
如果您只想查看错误,请添加一个回声,如下所示(或您可能想用它做的任何其他事情)。
class Swift_SwiftException extends Exception
{
/**
* Create a new SwiftException with $message.
*
* @param string $message
*/
public function __construct($message)
{
echo $message;
parent::__construct($message);
}
}