11

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?

d.h276
  • 137
  • 1
  • 2
  • 8
  • If `send()` does not return `false`, the message could be blocked by your recipient mail server. Did you check spam/junk folder? – Guillaume Poussel Oct 14 '13 at 18:12
  • Yea, nothing in the recipient's Junk folder and nothing in the sending email's Sent folder. But where would I find out if send() is returning false or not? – d.h276 Oct 14 '13 at 18:19
  • If you are not seeing any `"Error:"` followed by your `print_r` result, that means `send()` is returning `true` – Guillaume Poussel Oct 14 '13 at 18:21
  • @GuillaumePoussel Thanks but I don't think I'm at the right place to see "Error:" or anything else. What I have been doing is uploading the php file of that code onto my webserver and then nothing has happened. I am unable to go to it in my web browser to see any errors (gives me a 404) and I have not found how to view the errors via SSH. Where should I be looking for "Error:" or any other messages to appear? – d.h276 Oct 14 '13 at 18:35
  • When your web browser points to the PHP script you have attached. – Guillaume Poussel Oct 15 '13 at 05:56

3 Answers3

18

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.

Community
  • 1
  • 1
cyb0k
  • 2,478
  • 23
  • 19
8

Use transport exception handling like so:

    try {
        $mailer->send($message);
    }
    catch (\Swift_TransportException $e) {
        echo $e->getMessage();
    }
kjdion84
  • 9,552
  • 8
  • 60
  • 87
4

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);
    }
}
Jeff Davis
  • 4,736
  • 4
  • 38
  • 44