Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

php - Swift Mailer - Can't send mail, and can't find error logs

New to PHP and Swiftmailer and 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
        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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should try to use 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.

You only need to create new logger instance and add it to mailer object with registerPlugin() method.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...