Page 1 of 1

Call to member function on non-object

Posted: Mon Jul 30, 2007 5:10 am
by Kaitlyn2004
I am getting:

Code: Select all

Fatal error: Call to a member function isEnabled() on a non-object in .../Swift.php on line 407
It is checking if the log is enabled - swift has it set to null so I am unsure as to why swift would be trying to make that call? I don't have to use logging do I?

Using php 5.2.2... Swift class is wrapped in my custom class...

my class:

Code: Select all

require(ADMIN_ROOT . '/includes/swift_mailer/Swift.php');
require(ADMIN_ROOT . '/includes/swift_mailer/Swift/Connection/NativeMail.php');
require(ADMIN_ROOT . '/includes/swift_mailer/Swift/Connection/SMTP.php');
require(ADMIN_ROOT . '/includes/swift_mailer/Swift/Connection/Sendmail.php');

class Mailer extends Swift {
	var $mailer;
	var $message;
	
	function Mailer() {
		global $config;
	
		if ($config['DELIVERY_METHOD'] == 'php')
			$conn =& new Swift_Connection_NativeMail();
		else if ($config['DELIVERY_METHOD'] == 'sendmail')
			$conn =& new Swift_Connection_Sendmail(SWIFT_SENDMAIL_AUTO_DETECT);
		else if ($config['DELIVERY_METHOD'] == 'smtp') {
			$conn =& new Swift_Connection_SMTP($config['SMTP_HOST'], $config['SMTP_PORT']);
			
			if ($config['SMTP_USER'] != '') {
				$conn->setUsername($config['SMTP_USER']);
				$conn->setPassword($config['SMTP_PASS']);
			}
		}
		
		$this->mailer =& new Swift($conn);
		$this->message =& new Swift_Message();
		
		if ($config['BOUNCE_METHOD'] != 'none')
			$this->message->setReturnPath($config['BOUNCE_EMAIL']);
	}
}
the code:

Code: Select all

$mail =& new Mailer();
$message =& $mail->message;
$message->setSubject($subject);
$message->setFrom(new Swift_Address($fromEmail, $fromName));
$message->setReplyTo(new Swift_Address($fromEmail, $fromName));
$message->setCharset($charSet);
$message->setPriority($priority);

if ($format == 0)
	$message->setBody($textContent));
else if ($format == 1) {
	$message->setBody($tmp_htmlContent);
	$message->setContentType('text/html');
}
else if ($format == 2) {
	$message->attach(new Swift_Message_Part($textContent));
	$message->attach(new Swift_Message_Part($tmp_htmlContent, 'text/html'));
}

if (!$mail->send($message, $email, new Swift_Address($fromEmail, $fromName))) {
	echo 'failed!';
}

Posted: Mon Jul 30, 2007 6:11 am
by Chris Corbyn
The logger should be loaded even if it doesn't get used. Here's the code:

Code: Select all

/**
   * Constructor
   * @param Swift_Connection The connection object to deal with I/O
   * @param string The domain name of this server (the client) as a FQDN
   * @param int Optional flags
   * @throws Swift_ConnectionException If a connection cannot be established or the connection is behaving incorrectly
   */
  public function __construct(Swift_Connection $conn, $domain=false, $options=null)
  {
    $this->initializeEventListenerContainer();
    $this->setOptions($options);
    Swift_ClassLoader::load("Swift_Log_DefaultLog");
    $this->setLogger(new Swift_Log_DefaultLog());
    
    if ($this->hasOption(self::ENABLE_LOGGING)) $this->log->enable();

Code: Select all

/**
   * Set the logger to use
   * @param Swift_Log The instantiated logger
   */
  public function setLogger(Swift_Log $logger)
  {
    $this->log = $logger;
  }
I don't see a call to the log on line 407 in the latest version. What version of Swift are you using?

What version of Swift are you using?

Posted: Mon Jul 30, 2007 6:15 am
by Chris Corbyn
Ah I see, you're using the PHP4 version with PHP5. If you can do so, use the PHP5 version since I'm following the GoPHP5.org initiative and dropping PHP4 support in February 2008.

Line 407:

Code: Select all

$log_active = $this->log->isEnabled();

Code: Select all

/**
   * Constructor
   * @param Swift_Connection The connection object to deal with I/O
   * @param string The domain name of this server (the client) as a FQDN
   * @param int Optional flags
   * @throws Swift_ConnectionException If a connection cannot be established or the connection is behaving incorrectly
   */
  function Swift(&$conn, $domain=false, $options=null)
  {
    //Do I really have to check for simpletest stuff here??
    if (!is_a($conn, "Swift_Connection") && !is_a($conn, "SimpleMock"))
    {
      trigger_error("Swift requires constructor parameter 1 to be instance of Swift_Connection.");
      return;
    }
    $this->initializeEventListenerContainer();
    $this->setOptions($options);
    Swift_ClassLoader::load("Swift_Log_DefaultLog");
    $this->setLogger(new Swift_Log_DefaultLog());
    
    if ($this->hasOption($this->ENABLE_LOGGING)) $this->log->enable();

Code: Select all

/**
   * Set the logger to use
   * @param Swift_Log The instantiated logger
   */
  function setLogger(&$logger)
  {
    if (!is_a($logger, "Swift_Log"))
    {
      trigger_error("Swift::setLogger requires parameter 1 to be instance of Swift_Log.");
      return;
    }
    $this->log =& $logger;
  }
Unless that error in setlogger() ever gets thrown I can't really see how it would ever be a non-object without modification of the source code. Do you have set_error_handler() anywhere?

Posted: Mon Jul 30, 2007 7:53 am
by Kaitlyn2004
Unfortunately at this point I NEED to support PHP4...

there is no other call to set_error_handler appart from in /Swift/BatchMailer.php

Should I be making a specific call to load/enable logging, or what?

Posted: Mon Jul 30, 2007 7:59 am
by Kaitlyn2004
Weird... I don't recall changing a single thing, but now my error is:



Fatal error:
Uncaught Error of type [Swift_Connection_Exception] with message [There was a problem reading line 1 of an SMTP response. The response so far was:
[]. It appears the connection has died without saying goodbye to us! Too many emails in one go perhaps? (fsockopen: #0) ]
@0 require() in \msg_send_queue.php on line 80
@1 Mailer::Mailer() in \includes\msg_send.inc.php on line 15
@2 Swift::Swift() in \includes\mailer.class.php on line 27
@3 Swift::connect() in \includes\swift_mailer\Swift.php on line 111

in \includes\swift_mailer\Swift\Errors.php on line 99

Posted: Mon Jul 30, 2007 8:00 am
by Chris Corbyn
Kaitlyn2004 wrote:Unfortunately at this point I NEED to support PHP4...

there is no other call to set_error_handler appart from in /Swift/BatchMailer.php

Should I be making a specific call to load/enable logging, or what?
No you shouldn't need to do anything if you don't want to use logging. This is very odd and not something anyone else has brought up. Is this something that is reproduced every time you run the code? If so, could you private message, or email me a copy of the code so I can have a play with it?

I assume you've got swift working generally on the same server before you wrote the wrapper class?

Posted: Mon Jul 30, 2007 2:47 pm
by Kaitlyn2004
Sent you a PM

Posted: Mon Jul 30, 2007 2:48 pm
by Chris Corbyn
Thanks, I got the code. I'll give it a shot on my system and see what happens when I use PHP5 running the PHP4 swift :)

Posted: Tue Jul 31, 2007 12:37 pm
by Kaitlyn2004
Well, I'm stupid...(but I was tired and running on empty!)

The first thing I found was I had $mail->send... it should be $mail->mailer->send...

when I changed that I didn't receive "failed" or any other error, BUT I also didn't receive the email...

Posted: Wed Aug 01, 2007 3:01 am
by Chris Corbyn
Hi,

There are various reasons for non-rceipt of a message, but generally it boils down to spam blockers and/or blacklists. What domain did you send to? Without certain network configurations hotmail, yahoo and gmail are quite tricky -- hotmail in particular.

Have a read over this, in particular the blacklisting and the SPF record:

http://www.swiftmailer.org/wikidocs/v3/tips/spam

:)