Page 1 of 1

Call to a member function on a non-object

Posted: Mon Nov 26, 2007 7:04 pm
by BrianD
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Maybe someone can help.  I'm not a programmer, sometimes I run into a problem I can't resolve.  I installed Swiftmailer 3.3.2-php4, and have attempted to use the example of [b]Sending an email form with Swift[/b] from the tutorials section.  Once I get this working, I will work on the form and validation.  I resolved a couple of errors, but having trouble with this one.  I have a form.php that looks fine, but when I submit the form, I get a blank page, and the url indicates it is displaying handle_form.php.  My error log shows this error:

[Mon Nov 26 18:04:27 2007] [error] PHP Fatal error:  Call to a member function on a non-object in /home/bdonova1/public_html/handle_form.php on line 81. 

The PHP version on my shared hosting server is 4.3.11

Here is handle_form.php

Code: Select all

<?php
 
//Check if the required fields were sent
// Redirect back to the form if not
if (empty($_POST["sender_name"]) || empty($_POST["sender_email"])
    || empty($_POST["comment_title"]) || empty($_POST["comment_body"]))
{
    //redirect back to form
    header("Location: http://www.example.org/form.php?error=not_enough_info"); //This should really be an absolute URL if you know it
    exit();
}
 
//Copy into global variables
$name = $_POST["sender_name"];
$email = $_POST["sender_email"];
$title = $_POST["comment_title"];
$body = $_POST["comment_body"];
 
//Validate the email address using a regex (I suggest you use a better one than this!!)
if (!preg_match("/[a-zA-Z0-9_\\.-]+@[a-zA-Z0-9_\\.-]+/", $email))
{
    header("Location: http://www.example.org/form.php?error=invalid_email");
    exit();
}
 
//Check if an attachment was uploaded
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["attachment"]["tmp_name"]))
{
    if ($_FILES["attachment"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: http://www.example.org/form.php?error=upload_failed");
        exit();
    }
    $file_path = $_FILES["attachment"]["tmp_name"];
    $file_name = $_FILES["attachment"]["name"];
    $file_type = $_FILES["attachment"]["type"];
}
 
//Everything looks ok, we can start Swift
 
require_once "../swift/lib/Swift.php";
require_once "../swift/lib/Swift/Connection/SMTP.php";
 
//Enable disk caching if we can
if (is_writable("/tmp"))
{
    Swift_CacheFactory::setClassName("Swift_Cache_Disk");
    Swift_Cache_Disk::setSavePath("/tmp");
}
 
//Create a Swift instance

 
 $smtp =& new Swift_Connection_SMTP(
    "test.example.net", 25);
		$smtp->setUsername("user");
		$smtp->setpassword("password");
 
//$swift =& new Swift($smtp);

//Create the sender from the details we've been given
$sender =& new Swift_Address($email, $name);
 
//Create the message to send
$message =& new Swift_Message("New comment: " . $title);
$message->attach(new Swift_Message_Part($body));
 
//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
 
//Try sending the email
$sent = $swift->send($message, "user@exampleorg", $sender);
//Disconnect from SMTP, we're done
$swift->disconnect();
 
if ($sent)
{
    header("Location: http://www.example.org/success.php");
    exit();
}
else
{
    header("Location: http://www.example.org/form.php?error=sending_failed");
    exit();
}
[/i]

Line 81 is : $sent = $swift->send($message, "user@exampleorg", $sender);

I have obviously changed some of the URL and email addresses, but everything else is as shown. Any help would be appreciated.

Thanks,
Brian


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Nov 26, 2007 7:42 pm
by aaronhall

Code: Select all

//$swift =& new Swift($smtp);
$swift isn't being initialized

New Error

Posted: Mon Nov 26, 2007 8:07 pm
by BrianD
Thanks, I should've seen that. I think I commented that out troubleshooting this problem, which is now occurring again. It is line 64, but probably due to something just before that. Getting a connection refused now. I've tried my web hosting provider and smpt.gmail.com, same error 111. (see actual error below).



[Mon Nov 26 19:48:18 2007] [error] PHP Fatal error: <br /><strong>Uncaught Error</strong> of type [swift_connectionexception] with message [The SMTP connection failed to start [test.example.net:25]: fsockopen returned Error Number 111 and Error String 'Connection refused']<br /> @0 swift::swift() in /home/bdonova1/public_html/handle_form.php on line 64<br /> @1 swift::connect() in /home/bdonova1/swift/lib/Swift.php on line 109<br /><br /> in /home/bdonova1/swift/lib/Swift/Errors.php on line 99

Posted: Tue Nov 27, 2007 12:00 am
by aaronhall

Code: Select all

 $smtp =& new Swift_Connection_SMTP(
    "test.example.net", 25);
      $smtp->setUsername("user");
      $smtp->setpassword("password"); 

Posted: Wed Nov 28, 2007 8:53 pm
by Chris Corbyn
Looks like your host are blocking the connection. GoDaddy?

Go Daddy?

Posted: Thu Nov 29, 2007 7:32 am
by BrianD
No, it's not GoDaddy. Its dh2.net. I tried GMail as well and am getting the same error. I tried using the examples on the Docs page for GMail, the example for PHP 4, and then using the Authentication examples. Same error 111, connection refused, which I guessing is different from Permission Denied.

I've obviously not shown my real user names and passwords. But I should be able to get GMail working right?

Thanks,
Brian

Re: Go Daddy?

Posted: Fri Nov 30, 2007 12:45 am
by Chris Corbyn
BrianD wrote:No, it's not GoDaddy. Its dh2.net. I tried GMail as well and am getting the same error. I tried using the examples on the Docs page for GMail, the example for PHP 4, and then using the Authentication examples. Same error 111, connection refused, which I guessing is different from Permission Denied.

I've obviously not shown my real user names and passwords. But I should be able to get GMail working right?

Thanks,
Brian
If *every* connection you try is giving you connection refused it's definitely down to your host blocking the connection. Loads of shared hosting plans put you behind tight firewall rules which will prevent Swift Mailer (and other mailers) from working unless you use the NativeMail connection which simply wraps mail().