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.phpCode: 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();
}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]