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

前端 未结 3 1001
后悔当初
后悔当初 2020-12-16 18:57

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 uploade

相关标签:
3条回答
  • 2020-12-16 19:34

    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.

    0 讨论(0)
  • 2020-12-16 19:38

    Use transport exception handling like so:

        try {
            $mailer->send($message);
        }
        catch (\Swift_TransportException $e) {
            echo $e->getMessage();
        }
    
    0 讨论(0)
  • 2020-12-16 19:45

    Under your swift mailer extension there is an exception handler.

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题